A
ajmayo
I am new to Ruby and curious as to how you emulate the following
Javascript snippet
(example in Windows, hence the call to Echo)
var a = function(p) {WScript.Echo(p)}
bar(a);
function bar(z)
{
z(1);
WScript.Echo(z);
}
which would of course create an anonymous function, assign it to
variable a, pass this as a parameter to function bar() and then
evaluate the function with parameter 1, then attempt to print the
function itself (which Javascript will do, printing the text of the
block)
I found Ruby quite intuitive until I tried
a = {some block}
and found that this of course doesn't work as in this context {} refers
to a hash.
Ok, that's fine, but the 'yield' statement seems very funky and Perlish
to me. Effectively a block passed to a routine exists as a 'hidden'
argument so that
foo(100) {someblock}
in Ruby passes one parameter explicitly (as we would see from foo's
defined argument list) and a 'hidden' block which 'yield' inside the
body of foo() would evaluate.
(though, oddly, yield {someblock} is also not valid Ruby).
This seems horribly inelegant for a language touted as being The Next
Great Thing.
It is also unclear, how, then, I pass down a block as an argument and
then in turn pass it again to a child routine.
I can see how a parameter to a block works - this is clearly borrowed
from Smalltalk - but Javascript doesn't enforce separation of dynamic
code in the way Ruby appears to.
At present Javascript's syntax looks much cleaner. Am I missing
something?
Also, I presume Ruby is a forward-referencing language only, unlike
Javascript, where I can declare a function after code which calls it.
Ruby didn't seem to like that much.
Javascript snippet
(example in Windows, hence the call to Echo)
var a = function(p) {WScript.Echo(p)}
bar(a);
function bar(z)
{
z(1);
WScript.Echo(z);
}
which would of course create an anonymous function, assign it to
variable a, pass this as a parameter to function bar() and then
evaluate the function with parameter 1, then attempt to print the
function itself (which Javascript will do, printing the text of the
block)
I found Ruby quite intuitive until I tried
a = {some block}
and found that this of course doesn't work as in this context {} refers
to a hash.
Ok, that's fine, but the 'yield' statement seems very funky and Perlish
to me. Effectively a block passed to a routine exists as a 'hidden'
argument so that
foo(100) {someblock}
in Ruby passes one parameter explicitly (as we would see from foo's
defined argument list) and a 'hidden' block which 'yield' inside the
body of foo() would evaluate.
(though, oddly, yield {someblock} is also not valid Ruby).
This seems horribly inelegant for a language touted as being The Next
Great Thing.
It is also unclear, how, then, I pass down a block as an argument and
then in turn pass it again to a child routine.
I can see how a parameter to a block works - this is clearly borrowed
from Smalltalk - but Javascript doesn't enforce separation of dynamic
code in the way Ruby appears to.
At present Javascript's syntax looks much cleaner. Am I missing
something?
Also, I presume Ruby is a forward-referencing language only, unlike
Javascript, where I can declare a function after code which calls it.
Ruby didn't seem to like that much.