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.
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?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?Do you mean one method can only have one block?
someObject.someMethod({block1},{block2}) 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.Idiomatic Scheme basically never has "do" loops, despite their being part of the core standard, and it's been that way for decades.
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.
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 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
=> 3http://weblog.raganwald.com/2007/02/why-ruby-is-not-acceptab...
Either ruby blocks create a new scope or they do not. Which is it?
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.
I am only recently trying it out, and it seems to me Rails gives the line number of errors.
I'm not hating it, because despite all the stupid arguments, Python and Ruby are basically the same language.
But why do I still use Ruby? Because builtin Unicode doesn't matter to me that much, I can often answer questions from the Ruby source code faster than I can from Python's hard-to-search documentation, evented code in Ruby and Python is equivalently fast and everything else I do in C, and none of the other complaints from this article were valid. Against those points, Ruby's block construct is a huge win.
* Ruby's Unicode support is indeed inferior to Python's; unlike Python, you need to explicitly use library calls to work with UTF-16 strings. Python does indeed bake this in to the language.
* I don't use multiline regexps. On the other hand, I do use normal regexps, all the time, and the fact that I don't have to call out to a library to use them, or deal with special "match" objects with their own API, is a plus. Ruby has more natural support for regular expressions than Python, where regular expressions are an afterthought.
* Ruby's documentation sucks. Full stop.
* I have no idea why I'm meant to care about what this guy thinks of Ruby 1.9 or Ruby 2.0. There's a Ruby 2.0?
* Python is way faster than Ruby. Not by a little bit.
* Ruby's scoping rules are a dream compared to Python's; it support real closures, instance variables and methods with encapsulation, and coroutines that were designed for more than just iteration.
* Ruby and Python are equivalent when it comes to internal consistency. Python delegates virtually everything to a sprawling standard library for which there is, most certainly, "more than one way to do it". Calling out the difference between "print" and "puts" is particularly amusing if you know how Python handles the same distinction.
* Ruby has a cleaner object model than Python, where virtually every feature of the language appears to have been built by exposing the raw symbol table to the language. The simplest way to observe the difference is by taking an existing class and wrapping it with an object that catches all method invocations, a la "method_missing".
* Both Ruby and Python support keyword arguments by giving functions a dictionary type of keyword/value pairs; Ruby has the added benefit of a Symbol type, like Lisp and Smalltalk. No Ruby program fails to use them.
* I'm guessing about, oh, zero people on Hacker News give a shit about SAP support.
* I never noticed that Ruby didn't print the actual line of code that threw an exception. It's true! Python does! I never noticed that, either! That's pretty neat.
* Apparently, despite having been largely ported from Rails, Pylons is more mature than Rails. Awesome.