From ruby-talk-admin@ruby-lang.org Fri Dec 16 02:06:13 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 jBFH6DZk028759; Fri, 16 Dec 2005 02:06:13 +0900 Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 9A2CC59EE; Fri, 16 Dec 2005 02:06:17 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 8E03BF04842; Fri, 16 Dec 2005 02:06:17 +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 556C8F04850; Fri, 16 Dec 2005 02:06:16 +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 34A36630030; Fri, 16 Dec 2005 02:06:16 +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 E244933DC3; Fri, 16 Dec 2005 02:06:14 +0900 (JST) Received: from localhost (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 60A6133DC9 for ; Fri, 16 Dec 2005 02:06:07 +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 09272-10 for ; Fri, 16 Dec 2005 02:06:07 +0900 (JST) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.194]) by beryllium.ruby-lang.org (Postfix) with ESMTP id DBE6233DC3 for ; Fri, 16 Dec 2005 02:06:06 +0900 (JST) Received: by zproxy.gmail.com with SMTP id 8so406362nzo for ; Thu, 15 Dec 2005 09:06:01 -0800 (PST) Received: by 10.36.5.19 with SMTP id 19mr2019277nze; Thu, 15 Dec 2005 09:06:01 -0800 (PST) Received: by 10.36.71.19 with HTTP; Thu, 15 Dec 2005 09:06:01 -0800 (PST) Delivered-To: ruby-talk@ruby-lang.org Date: Fri, 16 Dec 2005 02:06:07 +0900 Posted: Thu, 15 Dec 2005 11:06:01 -0600 From: C Erler 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: <6a9c61b10512150906l1b317e1cj@mail.gmail.com> In-Reply-To: <1134665773.927049.245200@z14g2000cwz.googlegroups.com> References: <1134665773.927049.245200@z14g2000cwz.googlegroups.com> X-ML-Name: ruby-talk X-Mail-Count: 69 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= X-Original-To: ruby-talk@ruby-lang.org DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=qGdS06t5+Ymi3T9RRs90Y6pKzUQxJsoMfrTWqkKowjfHGKOdCSJhkHQdoTqMPpxGQxvqPyNehpEX8BT7JR+QKn5fA/BE7YwK5jywH1nwY7jJlaFykyCk2XEMJ8akPIxQ8SZ2w7gHrx9kZv28fSF0jKJ+dUcoYX93V7/7tzTqIug= Content-Disposition: inline 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=-9.5 required=7.0 tests=AWL,BAYES_00,BLARS00, BLARS_SPAM00,CONTENT_TYPE_PRESENT,MIMEQENC,QENCPTR1,QENCPTR2, RCVDFRMLOCALIP,RCVD_BY_IP,RCVD_IN_BLARS,RCVD_IN_BLARS_HOOPS, RCVD_IN_BLARS_SPAM autolearn=ham version=3.0.3 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Precedence: bulk Lines: 69 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 Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by blade.nagaokaut.ac.jp id jBFH6DZk028759 On 15/12/05, 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. > > 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. > > > Blocks that are objects are called procs, in Ruby (class Proc). To make one the way you want to, you simply put proc in front of it : my_block = proc { |vars| stuff_to_do } To use proc variables as blocks for methods, put &my_block, which lets Ruby know your proc isn't a normal parameter, it's the block : array.each &my_block