4.5 Exercises
-
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.
-
Using Listing 4.10 as a guide, add a shuffle method to the String class.
-
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.
-
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