Braces and begin/end 
Author Message
 Braces and begin/end

Hi!

I opened parse.y and made a serious try to understand why but my knowledge
of the parser generator language in parse.y is nil so I will once again
annoy you fellas with a newbie question:

The construction below does not work:
------------------------
a = 10
{
  print a, "\n"
  a -= 1

Quote:
} while a > 0

------------------------

Ruby says:
------------------------
Ruby1:4: parse error
  print a, "\n"
         ^
Ruby1:4: parse error
Ruby1:6: parse error

Quote:
} while a > 0

 ^(SyntaxError)

    from Ruby1
------------------------

But if I replace { and } with begin and end it works:

------------------------
a = 10
begin
  print a, "\n"
  a -= 1
end while a > 0
------------------------

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

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

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.

Robert



Wed, 25 Jun 2003 02:59:09 GMT  
 Braces and begin/end
Hi,

In message "[ruby-talk:8704] Braces and begin/end"

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

You can consider {} is almost equal to do/end which is used for block
passing to a method.  For example,

     a.each {|i| .... }

and

    a.each do |i| .... end

have identical meaning.

Quote:
>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.

It sounds delightful :-) I'm being trained here and in ruby-list now
too.  That is really great experience in my life.  These pleasant
communities are gem of Ruby, I think.  Thank you.

-- Gotoken



Wed, 25 Jun 2003 04:51:13 GMT  
 Braces and begin/end

Quote:

> Hi!

Hi too,

(...)

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

Of course not! ;-)

(...)

Quote:
> 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! ;-))

Quote:
> 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.

Quote:
> 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
;-))))

Quote:
> 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. :-)

Quote:
> Robert

\cle


Wed, 25 Jun 2003 06:14:20 GMT  
 Braces and begin/end
Hi,

In message "[ruby-talk:8704] Braces and begin/end"

|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.

It is my goal to build up friendly and respectful community as well as
making user friendly and amusing language.

                                                        matz.



Wed, 25 Jun 2003 11:51:03 GMT  
 Braces and begin/end

Quote:

>The construction below does not work:
>------------------------
>a = 10
>{
>  print a, "\n"
>  a -= 1
>} while a > 0

As others have pointed out, { } is not intended
to be used in this way.

I've been trying to develop my own Ruby style.
Since I came from a C++ world, I realized that
the curly braces (with dramatically different
meaning) would be confusing to me. So I don't use
them. Ever. I always use do/end, formatted like
this:

planets.each do
  | planet |
  # stuff goes here
end

It seems clean to me, so I'll keep doing it
unless/until I see a coding standard that I like
better.

Kevin



Wed, 25 Jun 2003 14:16:54 GMT  
 Braces and begin/end

Quote:

> I've been trying to develop my own Ruby style.
> Since I came from a C++ world, I realized that
> the curly braces (with dramatically different
> meaning) would be confusing to me. So I don't use
> them. Ever. I always use do/end, formatted like
> this:

The style I've come to is to use do/end for multi-line blocks, and
braces for single lines (where do/end seems to my eyes to be too much
noise). So I'd write

   arr.collect {|i| i+1}

and

   File.open("f") do |file|

     file.gets....

   end

Regards

Dave



Wed, 25 Jun 2003 14:36:02 GMT  
 Braces and begin/end
Hi,

Quote:
> From: Dave Thomas
> Sent: Saturday, January 06, 2001 3:36 PM
> > I've been trying to develop my own Ruby style.
> > Since I came from a C++ world, I realized that
> > the curly braces (with dramatically different
> > meaning) would be confusing to me. So I don't use
> > them. Ever. I always use do/end, formatted like
> > this:

> The style I've come to is to use do/end for multi-line blocks, and
> braces for single lines (where do/end seems to my eyes to be too much
> noise).

It's my turn.
value = {} if expecting its value, do/end if not.

Ex1.

  t = Thread.new {
    ...working...
  }

  Thread.new do
    sleep 60
    p 'timeout!'
  end

  t.join

Ex2.

  [ 1, 2, 3 ].collect { |n|
    n ** 2
  }.each do |n|
    p n
  end

// NaHi



Wed, 25 Jun 2003 15:05:42 GMT  
 Braces and begin/end

Hi,


Quote:
> I've been trying to develop my own Ruby style.
> Since I came from a C++ world, I realized that
> the curly braces (with dramatically different
> meaning) would be confusing to me. So I don't use
> them. Ever. I always use do/end, formatted like
> this:

Maybe you better take a closer look at the difference before deciding
that firmly :-)  According to the reference manual for Ruby 1.4, it is
boiled down to:

 - Braces has stronger precedence.

 - Braces introduce the nested local scopes.

Let me talk about the former (as later point is well demonstrated in
the manual).  The document says:

  foobar a, b do .. end   # foobar will be called with the block.
  foobar a, b { .. }      # b will be called with the block.

This, in turn, forms a syntax rule that "do .. end" can not be
followed by another method call.  (I am not sure if this is an
inevitable consequence of above precedence rule or just specific to
current implementation, though.)

For example, Ruby allow you to write something like bellow.

  # case-insensitive sort
  ["Foo", "bAr", "zoT"].collect{ |str|
    [str.downcase, str]
  }.sort.collect{ |pair|
    pair[1]
  }
  # => ["bAr", "Foo", "zoT"]

But you can not simply replace { .. } with do .. end here (coz, the
call of sort is not allowed after do .. end).

It is, of course, an option that never ever using chain like above.
But I personally feel like being more flexible :)

Minero Aoki has an interesting page discussing things like this at
length (Japanese only).

  http://www1.u-netsurf.ne.jp/~brew/mine/ja/ruby/style.html

Does anyone do the similar thing in English?

Regards,
Kenichi Komiya.



Wed, 25 Jun 2003 15:37:21 GMT  
 Braces and begin/end
Hello --

Quote:

> For example, Ruby allow you to write something like bellow.

>   # case-insensitive sort
>   ["Foo", "bAr", "zoT"].collect{ |str|
>     [str.downcase, str]
>   }.sort.collect{ |pair|
>     pair[1]
>   }
>   # => ["bAr", "Foo", "zoT"]

> But you can not simply replace { .. } with do .. end here (coz, the
> call of sort is not allowed after do .. end).

Have you tried it?  :-)

     ["Foo", "bAr", "zoT"].collect do |str|
        [str.downcase, str] end .sort.collect do |pair|
          pair[1]
        end

# => ["bAr", "Foo", "zoT"]

I know I've hit snags with doing this, though right now I can't seem
to recreate them.  But there does not seem to be any general rule against
chaining do/end blocks.

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 25 Jun 2003 19:33:38 GMT  
 Braces and begin/end

Quote:

> Let me talk about the former (as later point is well demonstrated in
> the manual).  The document says:

>   foobar a, b do .. end   # foobar will be called with the block.
>   foobar a, b { .. }      # b will be called with the block.

That then invokes my second style rule. If there's any potential
ambiguity, I'll but the parentheses around parameter lists.

Dave



Wed, 25 Jun 2003 21:44:50 GMT  
 Braces and begin/end

Thank you for the correction.


Quote:
> > But you can not simply replace { .. } with do .. end here (coz, the
> > call of sort is not allowed after do .. end).

> Have you tried it?  :-)

I actually tried it.  But apparently not hard enough :-(

I thought I used v1.7(CVS) for my trial, but it was v1.4.x (due to
stupid path setting).  I could reproduce this behavior (inability of
connect chain after "end") in 1.4.x.

Sorry for the confusion.  

Quote:

>      ["Foo", "bAr", "zoT"].collect do |str|
>         [str.downcase, str] end .sort.collect do |pair|
>           pair[1]
>         end

> # => ["bAr", "Foo", "zoT"]

> I know I've hit snags with doing this, though right now I can't seem
> to recreate them.  But there does not seem to be any general rule against
> chaining do/end blocks.

Maybe it was also v1.4.x.

Regards,
Komiya Kenichi.



Wed, 25 Jun 2003 21:36:15 GMT  
 Braces and begin/end

Quote:

>> From: Dave Thomas
>> The style I've come to is to use do/end for multi-line blocks, and
>> braces for single lines (where do/end seems to my eyes to be too much
>> noise).

That's not bad. I rarely do single-line stuff,
but when I do the braces would probably be
clearer.

Quote:
>It's my turn.
>value = {} if expecting its value, do/end if not.

>Ex1.

>  t = Thread.new {
>    ...working...
>  }

I kind of like this as well. Even though I don't
chain, there is benefit in making it clear
whether something is coming back or not. I'll
have to ponder this, and maybe experiment with
it.

Thanks to both of you for sharing these ideas!

Kevin



Thu, 26 Jun 2003 02:16:43 GMT  
 Braces and begin/end

Quote:


>> Let me talk about the former (as later point is well demonstrated in
>> the manual).  The document says:

>>   foobar a, b do .. end   # foobar will be called with the block.
>>   foobar a, b { .. }      # b will be called with the block.

>That then invokes my second style rule. If there's any potential
>ambiguity, I'll but the parentheses around parameter lists.

Or my style rule, which is that ALL parameter
lists get parens.

Kevin



Thu, 26 Jun 2003 02:13:04 GMT  
 Braces and begin/end

Quote:

> > But you can not simply replace { .. } with do .. end here (coz, the
> > call of sort is not allowed after do .. end).

> Have you tried it?  :-)

>      ["Foo", "bAr", "zoT"].collect do |str|
>         [str.downcase, str] end .sort.collect do |pair|
>           pair[1]
>         end

> # => ["bAr", "Foo", "zoT"]

> I know I've hit snags with doing this, though right now I can't seem
> to recreate them.  But there does not seem to be any general rule against
> chaining do/end blocks.

This improvement came with 1.6, I believe.

Hal



Thu, 26 Jun 2003 04:52:25 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. Cals Begin and End Balance

2. prob. with BEGIN/END

3. Smalltalk on the small end (was: Advice requested: GUI project beginning)

4. /BEGIN/ .. /END/ file reading

5. How to write filesearch with begin-pattern till end-pattern

6. begin/end doesn't behave as expected

7. Suggestion for addition to Begin/End syntax

8. Are begin/end blocks atomic?

9. Beginning of End of Ada Mandate

10. remove begin and end tags and its content in between

11. Is Perlish BEGIN / END possible?

12. Brackets in brackets, braces in braces

 

 
Powered by phpBB® Forum Software