Please explain this "Why's" example please

Discussion in 'Ruby' started by Kaye Ng, Jun 6, 2010.

  1. Kaye Ng

    Kaye Ng Guest

    I'm reading Why's Poignant Guide to Ruby.

    I don't understand this example.
    --------------------------------------------------------
    require 'wordlist'

    # Get evil idea and swap in code words

    print "Enter your new idea: "

    idea = gets

    code_words.each do |real, code|
    idea.gsub!( real, code )
    end
    ---------------------------------------------------------
    I'm assuming that, if I run the program, there would be a message on the
    screen that says "Enter your new idea:" and a text box for me to type
    something.

    After I type a word (and press an 'OK' button?), then what? Can anyone
    explain to me the role of Gets here? And also the Each block please. You
    may also give me another example with Gets and Each.

    Explain it to me as if I'm a moron please. No big words, no complicated
    programming terms, none of that. I'm a beginner.

    Thanks so much for your time. =)
    --
    Posted via http://www.ruby-forum.com/.
    Kaye Ng, Jun 6, 2010
    #1
    1. Advertising

  2. First things first there wouldn't be an entry box and an ok button, it
    would be on a command line (just text).
    gets is a method which when run will wait until the user enters
    something and then presses enter and then it returns what the user
    enters (so in the example what you enter gets stored in idea (which is a
    variable).
    each is a very useful method. In this example I assume code_words is a
    hash (you put lots of info in the hash, and give each piece of data a
    key so you can access it which is more data). For each piece of data the
    code between each and end is run (in this case idea.gsub!(real,code))
    with real being the key and code being the data.

    Example:
    If code_words was {"world"=>"planet", "hello"=>"hi"} and you input hello
    world then idea would be made to equal hi planet.
    Weirdly this code doesn't do anything with the idea once it has changed
    it but I assume that this is not the whole code or something.

    On 06/06/10 08:20, Kaye Ng wrote:
    > I'm reading Why's Poignant Guide to Ruby.
    >
    > I don't understand this example.
    > --------------------------------------------------------
    > require 'wordlist'
    >
    > # Get evil idea and swap in code words
    >
    > print "Enter your new idea: "
    >
    > idea = gets
    >
    > code_words.each do |real, code|
    > idea.gsub!( real, code )
    > end
    > ---------------------------------------------------------
    > I'm assuming that, if I run the program, there would be a message on the
    > screen that says "Enter your new idea:" and a text box for me to type
    > something.
    >
    > After I type a word (and press an 'OK' button?), then what? Can anyone
    > explain to me the role of Gets here? And also the Each block please. You
    > may also give me another example with Gets and Each.
    >
    > Explain it to me as if I'm a moron please. No big words, no complicated
    > programming terms, none of that. I'm a beginner.
    >
    > Thanks so much for your time. =)
    >
    Angus Hammond, Jun 6, 2010
    #2
    1. Advertising

  3. On Sun, Jun 6, 2010 at 12:50 PM, Kaye Ng <> wrote:
    >
    > After I type a word (and press an 'OK' button?), then what? Can anyone
    > explain to me the role of Gets here? And also the Each block please. You
    > may also give me another example with Gets and Each.


    First of all, it's "gets" and "each" - ruby is case sensitive :)

    Okay, so "gets" waits for the user to enter a line of text (that is,
    to type in a bunch of characters and then hit enter). It then returns
    that text as a string. Here's an example:

    while true
    print "say something: "
    a = gets
    puts "you entered #{a}"
    end

    To understand "each" you must first understand blocks. Every method in
    ruby has an implicit optional argument which is a block of code. The
    block is an anonymous function that is called by the method via the
    "yield" statement. An example will make it clearer:

    def run_a_block(arg1, arg2)
    puts "Argument 1 was #{arg1}"
    puts "Argument 2 was #{arg2}"
    puts "Now going to run the block with arguments foo and 42"
    yield ["foo", 42]
    puts "Okay, the block has run, now we are back in the run_a_block method"
    end

    run_a_block("hello", "world") do |x, y|
    puts "Now we are inside the block. run_a_block passed us arguments
    #{x} and #{y}"
    end

    The "do |x,y| ... end" bit is the block. The |x, y| is the argument
    list, and means that the calling method is expected to yield a list of
    two values. When 'yield' is called, control passes from the calling
    method to the block, and when it is done it returns to the line after
    yield.

    Okay, now for "each". "each" is a method of a collection. It expects a
    block, and yields each element of the collection in turn to the block.

    list = [1, 2, 4, 8, 16, 31]
    list.each do |number|
    puts "Got element #{number} from the list"
    end

    There is one more subtlety in _why's code example - when a block is
    passed a list of several elements, it can either capture them as a
    list or as individual elements (this is called "destructuring"). So if
    we call each on a hash table, which yields [key, value] pairs, we can
    say either

    h = {"hello" => "world", "foo" => "bar", "baz" => "quux"}
    h.each do |pair|
    puts "key is #{pair[0]}"
    puts "value is #{pair[1]}"
    end

    # or this way

    h. each do |key, value|
    puts "key is #{key}"
    puts "value is #{value}"
    end

    martin
    Martin DeMello, Jun 6, 2010
    #3
  4. On Jun 6, 2010, at 12:20 AM, Kaye Ng wrote:

    > I'm reading Why's Poignant Guide to Ruby.
    >=20
    > I don't understand this example.
    > --------------------------------------------------------
    > require 'wordlist'
    >=20
    > # Get evil idea and swap in code words
    >=20
    > print "Enter your new idea: "
    >=20
    > idea =3D gets
    >=20
    > code_words.each do |real, code|
    > idea.gsub!( real, code )
    > end
    > ---------------------------------------------------------
    > I'm assuming that, if I run the program, there would be a message on the
    > screen that says "Enter your new idea:" and a text box for me to type
    > something.
    >=20
    > After I type a word (and press an 'OK' button?), then what? Can anyone
    > explain to me the role of Gets here? And also the Each block please. You
    > may also give me another example with Gets and Each.


    Not everything in this world are text boxes and buttons ;-) -- there are go=
    od ol' input and output streams (character based) that are dealt with by me=
    ans of print, puts, gets and other methods of Ruby class IO. On Windows, wh=
    ich is most likely what you use, it is something you would see in and enter=
    from a cmd.exe window (sometimes called Console).

    Gennady.
    Gennady Bystritsky, Jun 7, 2010
    #4
  5. Kaye Ng

    Kaye Ng Guest

    From Why's example:
    _________________________________________________
    require 'wordlist'

    # Get evil idea and swap in code words

    print "Enter your new idea: "

    idea = gets

    code_words.each do |real, code|
    idea.gsub!( real, code )
    end
    __________________________________________________

    Where is the beginning of 'end'? Is it 'require'?
    (Like 'If' would be the beginning of an If statement (or is it block))

    Thanks guys!
    --
    Posted via http://www.ruby-forum.com/.
    Kaye Ng, Jun 8, 2010
    #5
  6. Kaye Ng

    Kaye Ng Guest

    Also, in Why's example, the words 'real' and 'code' are NOT variables,
    correct?
    Are they just random words that don't need to be defined? Like can I use
    'monkey' and 'ape' instead?


    --
    Posted via http://www.ruby-forum.com/.
    Kaye Ng, Jun 8, 2010
    #6
  7. Kaye Ng

    Kaye Ng Guest

    Difference between puts and print?

    Difference between puts and print, please? Thanks much!
    --
    Posted via http://www.ruby-forum.com/.
    Kaye Ng, Jun 8, 2010
    #7
  8. Re: Difference between puts and print?

    On Jun 7, 2010, at 11:30 PM, Kaye Ng <> wrote:
    > Difference between puts and print, please? Thanks much!


    Briefly, puts adds a newline at the end but print does not. Try them =
    both, the difference should be clear.

    Ben
    Ben Bleything, Jun 8, 2010
    #8
  9. Kaye Ng

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Jun 8, 2010 at 1:07 AM, Kaye Ng <> wrote:

    > From Why's example:
    > _________________________________________________
    > require 'wordlist'
    >
    > # Get evil idea and swap in code words
    >
    > print "Enter your new idea: "
    >
    > idea = gets
    >
    > code_words.each do |real, code|
    > idea.gsub!( real, code )
    > end
    > __________________________________________________
    >
    > Where is the beginning of 'end'? Is it 'require'?
    > (Like 'If' would be the beginning of an If statement (or is it block))
    >
    > Thanks guys!
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    The beginning is do. do ... end. This is called a code block. It is
    basically a method that you create in the middle of your code, as you need
    it. The method you are calling can in turn call your block to see how you
    want to handle some particular piece of code that should be custom to that
    one calling of the method. In your case, the method knows how to access each
    of the code words. But it doesn't know what you want to do with them. So you
    give it a block telling it what you want to do, and as it accesses the
    elements, it calls your block for each of them, passing them them into the
    block (your block says it is willing to receive them by placing "real" and
    "code" inside the pipes). In your case, the block looks through your idea
    and when it finds one of the real words, it replaces them with one of the
    code words. This brings up one of the important differences between blocks
    and methods, blocks can interact with the environment they are defined in.
    Methods cannot.

    On Tue, Jun 8, 2010 at 1:11 AM, Kaye Ng <> wrote:

    > Also, in Why's example, the words 'real' and 'code' are NOT variables,
    > correct?
    > Are they just random words that don't need to be defined? Like can I use
    > 'monkey' and 'ape' instead?
    >
    >

    Can you think of an easy way to find out? ^_^
    Josh Cheek, Jun 8, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. suzy
    Replies:
    3
    Views:
    437
  2. BluDog

    Can Someone Please Explain...

    BluDog, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    463
    BluDog
    Oct 5, 2004
  3. Alan Silver
    Replies:
    1
    Views:
    469
    Alan Silver
    Jun 28, 2005
  4. =?Utf-8?B?cmdyYW5kaWRpZXI=?=

    Please explain strange viewstate behavior...

    =?Utf-8?B?cmdyYW5kaWRpZXI=?=, Oct 26, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    421
    Eliyahu Goldin
    Oct 27, 2005
  5. Sam Roberts
    Replies:
    15
    Views:
    273
    Sam Roberts
    Feb 7, 2005
Loading...

Share This Page