On Sun, Nov 13, 2011 at 6:01 PM, Praveer wrote:
I am reading the book Ruby on Rails Tutorial : Learning by Example
At the end of chapter 4 theres a question. I tried many time but nit
succeeding in solving it.
Can you all please help me in writing a solution for the same?
Question is :
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
Listing 4.9. Skeleton for a string shuffle function :
Hi, to tackle such a question, I would use a combination of:
* irb (or pry)
* ruby documentation (e.g. http://www.ruby-doc.org/core/Array.html)
* use the .class and .inspect method intensively to study what type and
the result of each intermediate step is
So, in the session below, you see how I would build-up the result of your
peterv@ASUS:~$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
001:0> s = "foobar"
004:0> # now look up the documentation for String#split (http://www.ruby-doc.org/core-1.9.3/String.html)
005:0> s_split = s.split('')
=> ["f", "o", "o", "b", "a", "r"]
006:0> # now look up the documentation for Array#shuffle
007:0> s_split_shuffle = s_split.shuffle
=> ["o", "o", "f", "r", "a", "b"]
009:0> # now look up the documentation for Array#join
010:0> result = s_split_shuffle.join
012:0> # so the answer is s.split('').shuffle.join
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to firstname.lastname@example.org.
To unsubscribe from this group, send email to email@example.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.