Home > Articles > Programming > Ruby

  • Print
  • + Share This
This chapter is from the book

4.5 Exercises

  1. Using Listing 4.9 as a guide, combine the split, shuffle, and join methods to write a function that shuffles the letters in a given string.

  2. Using Listing 4.10 as a guide, add a shuffle method to the String class.

  3. Create three hashes called person1, person2, and person3, with first and last names under the keys :first and :last. Then create a params hash so that params[:father] is person1, params[:mother] is person2, and params[:child] is person3. Verify that, for example, params[:father] [:first] has the right value.

  4. Learn about the Hash method merge.

Listing 4.9. Skeleton for a string shuffle function

>> def string_shuffle(s)
>>   s.split('').?.?
>> end
=> nil
>> string_shuffle("foobar")

Listing 4.10. Skeleton for a shuffle method attached to the String class

>> class String
>>   def shuffle
>>     self.split('').?.?
>>   end
>> end
=> nil
>> "foobar".shuffle
  • + Share This
  • 🔖 Save To Your Account