Hi --

On Wed, 4 Aug 2010, Marnen Laibow-Koser wrote:

...which I do.  But for...in just doesn't feel like a good fit for Ruby
to me -- it feels more like syntactic sugar for Perl and PHP
programmers.

Evidence for that can be found in the fact that for is implemented in
terms of each:

  obj = Object.new
  def obj.each
    puts "Here I am in each, about to yield 100!"
    yield 100
  end

  for a in obj
    puts a
  end

Output:
  Here I am in each, about to yield 100!
  100

Another demo:

  $ ruby -e 'for a in 3; end'
  -e:1:in `<main>': undefined method `each' for 3:Fixnum (NoMethodError)

There's a slight difference in how they work, in the sense that each
takes a code block (at least, as usually implemented), while for is in
flat scope, similar to an if-statement:

  for a in [1]
    b = 100
  end

  p b   # 100

I don't know of any cases where having the flat scope would be so
important as to lead me to use for if I didn't have some other reason to
(which I don't think I ever have).


David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

  The                   Ruby training with Black/Brown/McAnally
  Compleat              Philadelphia, PA, October 1-2, 2010
  Rubyist               http://www.compleatrubyist.com

--
You received this message because you are subscribed to the Google Groups "Ruby on 
Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to