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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Peter Vandenabeele at Nov 13, 2011 at 10:25 pm

    On Sun, Nov 13, 2011 at 6:01 PM, Praveer 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Praveer at Nov 14, 2011 at 12:31 am

    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.c
    Thanks 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedNov 13, '11 at 9:48p
activeNov 14, '11 at 12:31a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Praveer: 2 posts Peter Vandenabeele: 1 post

People

Translate

site design / logo © 2021 Grokbase