3 ms·
> First of all the observation that most method only care about a single block. Except when they don't, but if you can't build multi-block methods (or methods
by masklinn 14y ago
> First of all the observation that most method only care about a single block.
Except when they don't, but if you can't build multi-block methods (or methods on blocks) because it looks like shit, you don't think about them. It's a form of blub, nothing more. There are numerous multi-block methods, Ruby's syntactic limitations just makes them unwieldy and thus avoided. Just as Python's or Java's syntactic limitations make higher-order methods unwieldy and thus avoided.
> By having custom syntax for "method that takes a single block" you get a certain simplicity that makes methods look like built-in keywords
Smalltalk stands in stark disproval of this assertion.
> It does make multi-block methods look ugly, but frankly, I think they look ugly in any language.
Eh?
aCondition ifTrue: [ "true block" ] ifFalse: [ "false block" ]
[ "some code" ]
ensure: [ "recovery code" ]
[ someObject aMessage ]
on: aSignal
do: [:sig | "action" ]
I don't think that looks ugly.
> Compared to JavaScript/Perl/Python-lambas, Ruby's blocks don't introduce another scope.
They do, if you define a variable within the block it'll be local to the block. Ruby 1.9 even allowed shadowing variables from the parent method (which isn't possible in 1.8)
> "self" [...] still refer to the method's scope
So does it with the vast majority of languages. Certainly does in Python. Javascript behaves strangely with its somewhat-dynamically-scoped-but-not-really `this` but it's a peculiarity of JS, not Ruby.
> and "return" still refer to the method's scope (I'm not sure what this type of scope is called)
It's a non-local return, not really a scope.
- judofyr 14y ago> Smalltalk stands in stark disproval of this assertion. I stand corrected. Although Smalltalk solves it by introducing a special method call syntax (which is perfectly fine). I was mostly thinking about C-style-method-call-syntax when I wrote this (Perl, Python, JS). > They do, if you define a variable within the block it'll be local to the block. Ruby 1.9 even allowed shadowing variables from the parent method (which isn't possible in 1.8) I did mention explicitly that I wasn't talking about variable scope, so yeah, we're both correct :)
- prodigal_erik 14y ago"Dynamic extent" might be close to what you're talking about. That's what Common Lisp calls the hint that an object may be stack allocated because it's unused after the call.
- masklinn 14y ago> Although Smalltalk solves it by introducing a special method call syntax (which is perfectly fine). Meh. Smalltalk comes from a time when not all languages had to be ALGOL-style, so it just has its own syntax. > I was mostly thinking about C-style-method-call-syntax when I wrote this (Perl, Python, JS). That is no issue, if you have first-class blocks. At most you'll have commas between your blocks because they'll be arguments like every other. Here's a python-ish version using chevrons for blocks: error = (request.form['username'] != app.config['USERNAME']).then(⟨ "Invalid Username" ⟩, else=⟨ None ⟩)