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
<animals> for 2 blocks in the same <rb> 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.
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
<animals> for 2 blocks in the same <rb> 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.
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".
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:
...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".
Sorry but I am trying to understand the flow here...
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...
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!"".