News for hackers. Stuff that matters.
Re:Ruby's not ready by akkartik on Monday April 07, 2008 @10:24PM (#157434)
Scoping. I got bitten by a huge bug in ruby's scoping last night:

    irb> [1,2,3].collect{|n| n+1}
    => [2, 3, 4]
    irb> n
    NameError: undefined local variable or method `n' for main:Object # Expected behavior - n's scope is restricted.
    irb> n = 0
    => 0
    irb> [1,2,3].collect{|n| n+1}
    => [2, 3, 4]
    irb> n
    => 3 # BAD
I have ruby 1.8.4. Most recent version is 1.8.6. Gotta go see if it's a known bug or worse if it's supposed to work this way..

Edit: elided profanity.

Re:Ruby's not ready by ambition on Monday April 07, 2008 @11:08PM (#157483)
It is supposed to work this way for some good reasons. But! Enough people have had the same problem as you that new versions of Ruby behave the way you expect.
Re:Ruby's not ready by raganwald on Tuesday April 08, 2008 @07:27AM (#157752)
Help me understand: what are the good reasons for the parameters of a block overwriting variables in an enclosing scope?

That is, we are talking about:

    i = 100
    x = 0
    [1,2,3].each { |i| x += i }
I understand why it is incredibly helpful for x to refer to a variable in the enclosing scope (even if we could accomplish this same effect with a fold). What are the use cases for the parameter i referring to a variable in the enclosing scope?
Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @08:48AM (#157799)
As someone who's used to Scheme (lexical scoping), I have to say that's pretty bizarre.

That's my second disappointment with Ruby blocks. The first was learning you could only have one block argument.

Do they resemble first-class objects at all? Can you assign a variable to a block? If you were to assign

  { |i| x += i }
to a variable outside that enclosing scope, and then used that variable in that scope, would i be overwritten in that call to the block?
Re:Ruby's not ready by raganwald on Tuesday April 08, 2008 @09:01AM (#157802)
"you could only have one block argument"

Do you mean one method can only have one block?

Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @09:06AM (#157803)
Having only read about Ruby and not coded in it, I'm not sure what the correct terminology is. I'm talking about doing something like this:

  someObject.someMethod({block1},{block2})
Re:Ruby's not ready by tptacek on Tuesday April 08, 2008 @09:25AM (#157820)
You can't pass more than one block literal to a method. But you can certainly pass multiple blocks; they're objects, just like anything else. Just add the token "lambda" to your expression:

   def foo(x, y); x.call(y.call); end
   foo(lambda {|x| 2+x}, lambda {1})  # => 3
It seems to me that in this case, the language degrades to approximately the same level as Common Lisp.
Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @09:59AM (#157840)
It's good to see that an upgrade to approximately the power of CL is possible in this case. Still, I feel a little let down. When I first saw Ruby block literals, I said to myself, "Hey, that's a great, concise syntax for lambda. Languages really are getting more lisp-like all the time." But now I know that Ruby block literals aren't really lambda. It does look like Ruby lambdas really are lambdas, though.
Re:Ruby's not ready by mdemare on Tuesday April 08, 2008 @10:20AM (#157866)
Blocks are lambda's, they're just not objects (but they can be converted to objects.)
Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @10:28AM (#157877)
If you can't pass more than one block literal to a method, then a block literal is not a real lambda, from the Lisp point of view.
Re:Ruby's not ready by tptacek on Tuesday April 08, 2008 @11:33AM (#157932)
This is confusing, because Lisp doesn't have the literal notation you're talking about. Since Ruby supports arbitrary lambdas using "lambda" notation (something Python does not support), it seems like Ruby's syntax can only be be better than Lisp's in this regard.
Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @12:37PM (#158001)
Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary. I do not think Ruby is better for having an additional syntax (block literals) that is just like an existing syntax (lambda) only more restricted.
Re:Ruby's not ready by tptacek on Tuesday April 08, 2008 @12:47PM (#158013)
The syntax isn't meant to be "a better lambda". The syntax is meant to recast other language functions, like looping and iteration, in terms of messages and anonymous functions. Since idiomatic Ruby basically never has "for" loops, despite their easy availability in the language, I'm going to go ahead and call the "block" expiriment successful.
Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @01:46PM (#158062)
If it's meant to recast certain language functionality in terms of anonymous functions, why didn't they just make block literals return anonymous functions? It still strikes me as an almost-could-have-been-great syntax.

Idiomatic Scheme basically never has "do" loops, despite their being part of the core standard, and it's been that way for decades.

Re:Ruby's not ready by tptacek on Tuesday April 08, 2008 @02:11PM (#158090)
I agree that block syntax should semantically result in expression evaluating to a Proc object. This would be great:

  y = {|x| x} # => #<Proc>
I agree that block syntax could therefore be greater than it is.

I disagree that you can fairly knock Ruby for this, since blocks and implicit block arguments are still better than what you get in other languages.

Re:Ruby's not ready by brlewis on Tuesday April 08, 2008 @09:54AM (#157834)
Could you give a more complete example? I'm getting sane behavior in Ruby 1.8.6:

  irb(main):010:0> f = lambda { i = 100; x = 0; [1,2,3].each { |i| x += i }; x}
  => #<Proc:0x05011b34@(irb):10>
  irb(main):011:0> f.call()
  => 6
Re:Ruby's not ready by raganwald on Tuesday April 08, 2008 @06:02PM (#158332)
you missed the fun:

    macbook-pro-2:trunk raganwald$ ruby -v
      => ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.10.1]
    macbook-pro-2:trunk raganwald$ irb
    irb(main):001:0> lambda { i = 100; x = 0; [1,2,3].each { |i| x += i }; i }.call
      => 3
Re:Ruby's not ready by ambition on Tuesday April 08, 2008 @07:52AM (#157768)
Oh, you're right, I had missed that. Yeah, that's dumb. I can't think of any iterators that would leave useful information in the parameters, either.
Re:Ruby's not ready by raganwald on Tuesday April 08, 2008 @07:21AM (#157751)
Yes, it is a known problem, and I have also found myself editing my own profanity:

http://weblog.raganwald.com/2007/02/why-ruby-is-not-acceptab...

Re:Ruby's not ready by tptacek on Monday April 07, 2008 @10:41PM (#157449)
Your "bug" is lexical scoping?
Re:Ruby's not ready by akkartik on Monday April 07, 2008 @10:55PM (#157461)
No, it's lack of consistent lexical scoping.

Either ruby blocks create a new scope or they do not. Which is it?

Re:Ruby's not ready by tptacek on Monday April 07, 2008 @10:58PM (#157466)
They create new scopes, and close over the their enclosing environment. In other words, it supports both block-local variables and closures.
Re:Ruby's not ready by akkartik on Monday April 07, 2008 @11:00PM (#157470)
I don't understand. You mean there's a good reason for it to overwrite variables in outer scopes if they exist?

Is there any way to specify if I want block-local or not?

The way python deals with scopes is to provide read access to outer scopes if there are no writes to a variable.

Re:Ruby's not ready by tptacek on Monday April 07, 2008 @11:02PM (#157475)
Of course there is: if you couldn't access variables in the enclosing scopes, blocks would be drastically less useful. Ruby uses anonymous functions where Python would use a loop construct.
Re:Ruby's not ready by akkartik on Monday April 07, 2008 @11:16PM (#157492)
[We took this conversation offline. it seems this is a known gotcha (e.g. http://www.osix.net/modules/article/?id=401), and may be changed in ruby 2.0 (http://groups.google.com/group/sbonrails/browse_thread/threa...). Thanks tptacek.]