The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ruby 2.0 block local variable

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Ruby 2.0 block local variable Posted: Mar 16, 2005 2:20 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Ruby 2.0 block local variable
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Translated from matz blog, 2005-03-09

This is a topic I wanna talk about the meeting on yesterday1 but I didn’t because it didn’t warm up so much.

I’ve been thinking about Ruby’s block scope for a long time. I couldn’t single out easily in various ideas. In these days, It’s thought that I find the one hit the spot.

The specification is as follows.

  • Block parameters are only local valiables.
  • The scope of block parameter is block local.
  • If the name of block parameter is overlapped outside local variable, then it’s error.(Shugo Maeda won.)
  • You can set a variable that is enable in the block using syntax like ”; var” after block parameters.
  • Overlapping of these variables is error.
  • The scope of a variable appears first in the block is block local (according to the current).
  • The reference to the variable in which the scope used in the method has ended is warned.

Though it’s different that I’ve declared about Ruby 2.0 upto now (especially, part of strong-ed), I seem the compatibility is higher and the usability is not worsend. Especially, dblack has been opposed applying the method scope in the point of “The scope of a variable appears first” might be pleased.

Now, as a result,

3.times {|i; a|
  a = 5
  p [i, a]
}

is ok.

3.times {|i; a=5|
  p [i, a]
}

A syntax with initializing is more better. But ”|” is the operator might be in an expression. Thus,

  • We give it up.
  • Only primary one(literal, variable, expression bundled by parentheses, etc) be permitted at initialization.

It seems to become either.

As for the rest, it depends on whether we accept that the below is error or not.

3.times {|i; a=2+3|
  p [i, a]
}

1 Matz organized a mini meeting Meetup for The Matsue Ruby Meetup Group . Matz belongs among Matsue Ruby Meetup Group .

Read: Ruby 2.0 block local variable

Topic: Annotations in Ruby Previous Topic   Next Topic Topic: A week worth of updates

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use