Hello all, 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 string. Listing 4.9. Skeleton for a string shuffle function :>> def string_shuffle(s) >> s.split('''').?.? >> end=> nil>> string_shuffle("foobar")Thank you -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Nov 13, 2011 at 6:01 PM, Praveer <praveerdixit2007-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello all, > > 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 > string. > > Listing 4.9. Skeleton for a string shuffle function : > > >> def string_shuffle(s) > >> s.split('''').?.? > >> end > => nil > >> string_shuffle("foobar") >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 content the result of each intermediate step is So, in the session below, you see how I would build-up the result of your excersize: peterv@ASUS:~$ ruby -v ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] peterv@ASUS:~$ irb 001:0> s = "foobar" => "foobar" 002:0> s.class => String 003:0> s.inspect => "\"foobar\"" 004:0> # now look up the documentation for String#split ( http://www.ruby-doc.org/core-1.9.3/String.html) 005:0* ^C 005:0> s_split = s.split('''') => ["f", "o", "o", "b", "a", "r"] 006:0> # now look up the documentation for Array#shuffle 007:0* ^C 007:0> s_split_shuffle = s_split.shuffle => ["o", "o", "f", "r", "a", "b"] 008:0> s_split_shuffle.class => Array 009:0> # now look up the documentation for Array#join 010:0* ^C 010:0> result = s_split_shuffle.join => "oofrab" 011:0> result.class => String 012:0> # so the answer is s.split('''').shuffle.join 013:0* ^C HTH, Peter -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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 > content > the result of each intermediate step is > > So, in the session below, you see how I would build-up the result of your > excersize: > > peterv@ASUS:~$ ruby -v > ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux] > peterv@ASUS:~$ irb > 001:0> s = "foobar" > => "foobar" > 002:0> s.class > => String > 003:0> s.inspect > => "\"foobar\"" > 004:0> # now look up the documentation for String#split ( > http://www.ruby-doc.org/core-1.9.3/String.html) > 005:0* ^C > 005:0> s_split = s.split('''') > => ["f", "o", "o", "b", "a", "r"] > 006:0> # now look up the documentation for Array#shuffle > 007:0* ^C > 007:0> s_split_shuffle = s_split.shuffle > => ["o", "o", "f", "r", "a", "b"] > 008:0> s_split_shuffle.class > => Array > 009:0> # now look up the documentation for Array#join > 010:0* ^C > 010:0> result = s_split_shuffle.join > => "oofrab" > 011:0> result.cThanks a lot mate. I was doing maximum right, but the way I was usaging the split and shuffle was wrong. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.