Basic question about blocks

Hello forum,

I am new to Ruby and have a question about blocks.

A) Below is the code I have put in a file <blocksexample.rb>:


Script block2.rb

def simpleFunction
yield
yield
end

animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

B) I run the <blocksexample.rb> as below…only once:

C:\RUBYEX~1>blocksexample.rb

C) The output is as below:

Hello, Tiger
Hello, Giraffe
It’s 5 characters long!
It’s 7 characters long!

Question 1: How is it I am not getting error for using same name
for 2 blocks in the same file?
Question 2: How is the first animal block executed followed by the
second animal block execution…inspite of me running the example only
once…

Please let me know, appreciate your help in advance.

Esh

On Mon, May 13, 2013 at 9:03 AM, Esh G. [email protected] wrote:

  yield

Question 2: How is the first animal block executed followed by the
second animal block execution…inspite of me running the example only
once…

Please let me know, appreciate your help in advance.

Can you check the program you posted? There is no animals method, so it
blows up on my computer.

-Josh

Josh,

Sorry for mistake in typing the code…below is the program code…


Script block1.rb

def animals
yield “Tiger”
yield “Giraffe”
end

animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

And the Yield function in above …

  • first yields to the … animals { |x| puts “Hello, #{x}” } for
    “Tiger”
  • then the same “Tiger” should go to the … animals { |x| puts “It’s
    #{x.length} characters long!” }

… this is my understanding…but the code prints out as below:

Hello, Tiger
Hello, Giraffe
It’s 5 characters long!
It’s 7 characters long!

…Meaning the

  • first animals block executes for the word “Tiger” and “Giraffe”
    FOLLOWED BY
  • second animals block executes for the word “Tiger” and “Giraffe”

The order of execution is correct, you’re calling “animals” twice, and
in each case you’re getting “Tiger” and “Giraffe” before moving on the
next call of “animals”.

Joel P. wrote in post #1108788:

The order of execution is correct, you’re calling “animals” twice, and
in each case you’re getting “Tiger” and “Giraffe” before moving on the
next call of “animals”.

Sorry but I am trying to understand the flow here…

  1. First yield "Tiger" should call
    

animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

  1. Second yield "Giraffe" should call
    

animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

But the output is NOT…
Hello, Tiger
It’s 5 characters long!
Hello, Giraffe
It’s 7 characters long!

The output IS…

Hello, Tiger
Hello, Giraffe
It’s 5 characters long!
It’s 7 characters long!

Actually, Ruby is doing exactly what you are asking it to do.

You need to rewrite your block to get what you are looking for.

animals { |x| puts “Hello, #{x}\nIt’s #{x.length} characters long!” }

When you were calling animals, it stepped through both yields. That’s
expected. Why do you think it should have been otherwise?

Wayne

The output is correct, to get what you want to change the sentence:

 animals { |x| puts "Hello, #{x}"; puts "It's #{x.length}

characters long!" }

Saludos,
Ulises

2013/5/13 Esh G. [email protected]:

Or this:

animals { |x| puts “Hello, #{x}\nIt’s #{x.length} characters long!” }

You call twice the animal function. The first time sent “puts “Hello,
#{x}”” as a block and the second time “puts “It’s #{x.length}
characters long!””.

Saludos,
Ulises

2013/5/13 Esh G. [email protected]:

Yes, thats what I thought :slight_smile:

Thanks Ulises and everybody.

So long,
Esh

Joel P. wrote in post #1108793:

Or this:

animals { |x| puts “Hello, #{x}\nIt’s #{x.length} characters long!” }

Ok. Let me undersdtand this…

  1. I was thinking the in the below function

def animals
yield “Tiger”
yield “Giraffe”
end

… calls each of the blocks below with “Tiger” and “Giraffe”


animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

  1. But I think from all the explanations I got… below might be true

animals { |x| puts “Hello, #{x}” }
animals { |x| puts “It’s #{x.length} characters long!” }

… calls the function


def animals
yield “Tiger”
yield “Giraffe”
end

Does this make any sense ?

Esh