"Robert Gustavsson" <robertg / swipnet.se> wrote:
> Hi!

Hi too,

(...)

> ------------------------
> a = 10
> {
>   print a, "\n"
>   a -= 1
> } while a > 0
> ------------------------

Of course not! ;-)

(...)

> But if I replace { and } with begin and end it works:
> 
> ------------------------
> a = 10
> begin
>   print a, "\n"
>   a -= 1
> end while a > 0
> ------------------------

Of course, yes! ;-))

> Silly me thought that {} and begin/end was synonyms.

Nothing to silly you!  Perhaps you was trapped because the usage of
begin/end in Pascal compared to that of {} in C?

But in Ruby the braces are synonym for do/end, not for begin/end. 
Braces (like do/end) form blocks (some wrapped code) that you can call
via statement yield or that you can convert into a Proc instance via
Proc.new, proc or lambda. Such instances could be called by you or
carried with you whereever you want (hint: closure) :-)

However begin/end form a total different construct that has nothing to
do with blocks (those formed via {} or do/end).

Small example:

  def myFor(init, limit, step=1)
    while init < limit
      yield init        # This will call the attached block
      init += step
    end
    nil
  end

or

  def myFor(init, limit, step=1)
    block = Proc.new    # or use proc or lambda. Convert 
                        # attached block to Proc instance
    while init < limit
      block.call(init)  # This will call the closure
      init += step
    end
    nil
  end

or even

  def myFor(init, limit, step=1, &block) # &block takes/convert
                                         # attached block
    while init < limit
      block.call(init)  # This will call the closure
      init += step
    end
    nil
  end

All these methods will work equally, *if* you do not forget to attach
a block to it during invocation. The block you have to attach has to
be able to receive one argument. Usage is like:

  myFor(1,10) { |i| puts "i=#{i}" }

or

  myFor(1,10) do |i| 
    puts "i=#{i}"
  end

or even

  block = proc { puts "i=#{i}" }
  myFor(1,10,&block)

etc. pp.


> Apparantly they aren't. Why not? Any other pitfalls about {} and
> begin/end one should be aware of?

Only those you have also aware of, if you compare eggs with apples
;-))))

> I must say that ruby-talk is a very positive experience so far. Very
> prompt and competent replies and very kind and helpful
> people. Hopefully I will gain some knowledge about Ruby to be able
> to contribute.

This will certainly happen very fast, I believe. Ruby is one of the
easiest languages I know of, so far. In a short time you will master
Ruby and seeing yourself answering questions to help others too. :-) 

> Robert

\cle