--7JfCtLOvnd9MIVvH Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Fri, Dec 16, 2005 at 01:57:41AM +0900, ajmayo / my-deja.com wrote: > 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. You can use: a = lambda {some block} > Ok, that's fine, but the 'yield' statement seems very funky and Perlish > to me. Sorry, I don't see the connection :-? > 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). yield is to _call_ a given block. You do things like: def foo(bar) yield "foo, #{bar}!" end foo("world") do |i| puts i end > 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. Easy: def some_method yield "some value" end def foo(bar, &blk) some_method(&blk) end foo(1) do |i| puts i end I.e. every time you put an ampersand before a parameter when defining some method, you get the block as a Proc object. Every time you put an ampersand before a parameter when calling some method, the Proc object is received as a regular block by the callee. Take a look at the first edition of Pickaxe. It's publicly available at http://www.ruby-doc.org/docs/ProgrammingRuby/. > 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? Hope the above clears up some confusion. > 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. So, why not just use Javascript? :-) -- Esteban Manchado VeláÛquez <zoso / foton.es> - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es --7JfCtLOvnd9MIVvH Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (GNU/Linux) iD8DBQFDoa9AhYgK5b1UDsERAsddAJwLNIJ+JsgWMj0g0AHN2B89XVw7XwCZATMu 8axKqbwsxOYivxGzI6UcFrU yc -----END PGP SIGNATURE----- --7JfCtLOvnd9MIVvH--