From ruby-talk-admin@ruby-lang.org Fri Dec 16 03:01:15 2005 Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (8.12.3/8.12.3/Debian-6.6) with ESMTP id jBFI1FZk030438; Fri, 16 Dec 2005 03:01:15 +0900 Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 364825AC8; Fri, 16 Dec 2005 03:01:20 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 51E2BF04866; Fri, 16 Dec 2005 03:01:20 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 67DEDF0489F; Fri, 16 Dec 2005 03:01:09 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [210.163.138.100]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 73684630024; Fri, 16 Dec 2005 03:01:09 +0900 (JST) Received: from beryllium.ruby-lang.org (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 4227E33F4B; Fri, 16 Dec 2005 03:00:57 +0900 (JST) Received: from localhost (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id CF58533F1E for ; Fri, 16 Dec 2005 03:00:44 +0900 (JST) Received: from beryllium.ruby-lang.org ([127.0.0.1]) by localhost (beryllium.ruby-lang.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 17253-07 for ; Fri, 16 Dec 2005 03:00:44 +0900 (JST) Received: from delta.lanzanet.net (unknown [67.15.20.49]) by beryllium.ruby-lang.org (Postfix) with ESMTP id BBA2E33DF9 for ; Fri, 16 Dec 2005 03:00:43 +0900 (JST) Received: from demiurgo.org (68.Red-80-59-103.staticIP.rima-tde.net [80.59.103.68]) by delta.lanzanet.net (Postfix) with SMTP id 7784F4CC5EA for ; Thu, 15 Dec 2005 12:00:34 -0600 (CST) Received: (nullmailer pid 16413 invoked by uid 1000); Thu, 15 Dec 2005 18:00:32 -0000 Delivered-To: ruby-talk@ruby-lang.org Date: Fri, 16 Dec 2005 03:00:45 +0900 Posted: Thu, 15 Dec 2005 18:00:32 +0000 From: Esteban Manchado =?iso-8859-1?Q?Vel=E1zquez?= Reply-To: ruby-talk@ruby-lang.org Subject: Re: Assigning a block to a variable in Ruby To: ruby-talk@ruby-lang.org (ruby-talk ML) Message-Id: <20051215180032.GC6110@demiurgo.org> In-Reply-To: <1134665773.927049.245200@z14g2000cwz.googlegroups.com> References: <1134665773.927049.245200@z14g2000cwz.googlegroups.com> X-ML-Name: ruby-talk X-Mail-Count: 88 X-MLServer: fml [fml 4.0.3 release (20011202/4.0.3)]; post only (only members can post) X-ML-Info: If you have a question, send e-mail with the body "help" (without quotes) to the address ruby-talk-ctl@ruby-lang.org; help= User-Agent: Mutt/1.5.11 X-Original-To: ruby-talk@ruby-lang.org Content-Disposition: inline X-Sender: Esteban Manchado =?iso-8859-1?Q?Vel=E1zque?= =?iso-8859-1?Q?z?= X-GPG: http://www.demiurgo.org/zoso.gpg X-lanzanet-MailScanner-Information: Please contact the ISP for more information X-lanzanet-MailScanner: Found to be clean X-MailScanner-From: zoso@foton.es X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ruby-lang.org X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on beryllium.ruby-lang.org X-Spam-Level: X-Spam-Status: No, score=-12.2 required=7.0 tests=AWL,BAYES_00,BLARS00, BLARS_SPAM00,CONTENT_TYPE_PRESENT,MIMEQENC,QENCPTR1,QENCPTR2, RCVDFRMLOCALIP,RCVD_IN_BLARS,RCVD_IN_BLARS_ABUSE,RCVD_IN_BLARS_SPAM, RCVD_IN_BLARS_SPAM_WEB autolearn=ham version=3.0.3 Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="7JfCtLOvnd9MIVvH" Precedence: bulk Lines: 127 List-Id: ruby-talk.ruby-lang.org List-Software: fml [fml 4.0.3 release (20011202/4.0.3)] List-Post: List-Owner: List-Help: List-Unsubscribe: X-Virus-Scanned: by AMaViS snapshot-20020531 --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) >=20 > var a =3D function(p) {WScript.Echo(p)} >=20 > bar(a); >=20 > function bar(z) > { > z(1); > WScript.Echo(z); > } >=20 > 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) >=20 > I found Ruby quite intuitive until I tried >=20 > a =3D {some block} >=20 > and found that this of course doesn't work as in this context {} refers > to a hash. You can use: a =3D 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 >=20 > foo(100) {someblock} >=20 > 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. >=20 > (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. >=20 > 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. >=20 > 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? :-) --=20 Esteban Manchado Vel=E1zquez - 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= =Hmyc -----END PGP SIGNATURE----- --7JfCtLOvnd9MIVvH--