From: Josh Cheek Date: 2013-05-13T23:19:21+09:00 Subject: Re: Basic question about blocks --089e0158b87ad41c1f04dc9a333e Content-Type: text/plain; charset=ISO-8859-1 On Mon, May 13, 2013 at 9:03 AM, Esh Gun wrote: > Hello forum, > > I am new to Ruby and have a question about blocks. > > A) Below is the code I have put in a file : > > --------------------------------------------------- > # 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 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. > > Can you check the program you posted? There is no animals method, so it blows up on my computer. -Josh --089e0158b87ad41c1f04dc9a333e Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Mon, May 13, 2013 at 9:03 AM, Esh Gun <lists@ruby-forum.com><= /span> wrote:
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
=A0 =A0 =A0 yield
=A0 =A0 =A0 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
<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.


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

-Josh=
--089e0158b87ad41c1f04dc9a333e--