5 ms·
So, Gary says both "I can't stand Ruby's Syntactic Flexibility" and "I wish Python had more Syntactic Flexibility." (Really, all he wants are blocks so that he
by trebor 16y ago
So, Gary says both "I can't stand Ruby's Syntactic Flexibility" and "I wish Python had more Syntactic Flexibility." (Really, all he wants are blocks so that he can have RSpec, etc.) But he does point out some odd, and some good features of Ruby that are worth attention.
Make up your mind, please. ;)
His example that:
>> x = Proc.new { 5}
=> #<Proc:0x00000001015f21a0@(irb):1>
>> x()
NoMethodError: undefined method `x' for main:Object
from (irb):2
>> x[]
=> 5
>> x.call
=> 5
Is also a bit crazy: Proc.[] is an instance method of a Proc that is aliased to Proc.call (crazy as that may be); proc() is a language feature for calling an object's method (a proc isn't a method, it's an object--Proc.call is the method). And before you ask what about a method without an object, understand that they do not exist. There is a global object that receives all methods not associated with an object.
>> def x
>> p self
>> end
=> nil
>> y = Proc.new { p self }
=> #<Proc:0x00000001015ecf48@(irb):4>
>> x()
main
=> nil
>> y.call
main
=> nil
And, in case you're confused about why y.call prints "main", the Proc's block is technically created within the scope of the calling or parent object. In other words, the block passed to the Proc is created in the main class and then delegated to the Proc instance--therefore when executed it belongs to, and references the scope it was created in: main. (A binding from the Proc itself is neither created, nor passed to the block. If it were the block would return the same Proc inspection from line 4.)
And... the reason a "patch level release" (his words, not mine) added syntax is because 1.8.7 is a backport of new features from Ruby 1.9 down to Ruby 1.8. I don't claim to like or understand the version numbers, but the patch level is given like "ruby-1.9.2-p136" not as the third part of the number itself.
/rant
Sorry, I should stop here before I lose my whole morning.
- xtho 16y agoAnd before you ask what about a method without an object, understand that they do not exist. Technically, there are unbound methods (i.e. methods with no object) but you can't call them.
- trebor 16y agoTechnically they exist, but I think it's an oxymoron because it must be bound to be usable. To be honest, I still can't figure out a reason you'd ever use one. Do you have an idea of when they're useful?
- stephencelis 16y agoPerhaps more technically, unbound methods _are_ objects (as are [bound] methods, procs, etc.).
- xtho 16y agoYou are right of course.