From ruby-talk-admin@ruby-lang.org Thu Dec 15 19:52:52 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 jBFAqqZk010602; Thu, 15 Dec 2005 19:52:52 +0900 Received: from funfun.nagaokaut.ac.jp (funfun.nagaokaut.ac.jp [133.44.2.201]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 4F82059E0; Thu, 15 Dec 2005 19:52:53 +0900 (JST) Received: from localhost (localhost.nagaokaut.ac.jp [127.0.0.1]) by funfun.nagaokaut.ac.jp (Postfix) with ESMTP id 52228F04842; Thu, 15 Dec 2005 19:52:56 +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 01654F0484A; Thu, 15 Dec 2005 19:52:55 +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 1C25263002A; Thu, 15 Dec 2005 19:52:56 +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 BC9F933D20; Thu, 15 Dec 2005 19:52:51 +0900 (JST) Received: from localhost (beryllium.ruby-lang.org [127.0.0.1]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 973E333EA2 for ; Thu, 15 Dec 2005 19:52:42 +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 00360-09 for ; Thu, 15 Dec 2005 19:52:42 +0900 (JST) Received: from sinus.lauschmusik.de (sinus.lauschmusik.de [213.95.32.201]) by beryllium.ruby-lang.org (Postfix) with ESMTP id 2434833D83 for ; Thu, 15 Dec 2005 19:52:42 +0900 (JST) Received: from localhost ([127.0.0.1] helo=localhost.localdomain) by sinus.lauschmusik.de with esmtp (Exim 4.50) id 1EmqgW-0004Xi-Dv for ruby-talk@ruby-lang.org; Thu, 15 Dec 2005 11:50:04 +0100 Received: from ptn-nntp-reader02.plus.net (ptn-nntp-reader02.plus.net) by sinus.lauschmusik.de with NTTP id 166174 for ; Thu Dec 15 11:50:04 CET 2005 Received: from [d8c6eb6d.ptn-nntp-reader02.plus.net] by ptn-nntp-reader02.plus.net (unknown) with NNTP id 166174 for ; Thu Dec 15 11:50:04 CET 2005 Received: from Usenet via a Usenet to mail gateway located at lauschmusik.de. This service provided as a courtesy to the ruby-talk mailing list. If this message is SPAM, its ultimate origin is Usenet, not this gateway program. All subscribers to the ruby-talk mailing list agree to receive the Usenet postings made to comp.lang.ruby via this gateway. Please see http://www.ruby-lang.org/ruby-talk-usenet-policy.html. Delivered-To: ruby-talk@ruby-lang.org Date: Thu, 15 Dec 2005 19:52:43 +0900 Posted: Thu, 15 Dec 2005 10:46:20 -0000 From: "Ross Bamford" Reply-To: ruby-talk@ruby-lang.org Subject: Re: treating 'string'+var as method To: ruby-talk@ruby-lang.org (ruby-talk ML) Message-Id: References: X-ML-Name: ruby-talk X-Mail-Count: 7 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: Opera M2/8.50 (Linux, build 1358) X-Original-To: ruby-talk@ruby-lang.org Path: noris.net!news.n-ix.net!news.rh-tec.net!newsfeed.stueberl.de!newsgate.cistron.nl!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!newsfeeder.wxs.nl!nntp-peering.plus.net!ptn-nntp-feeder01.plus.net!ptn-nntp-spool02.plus.net!ptn-nntp-reader02.plus.net!not-for-mail Newsgroups: comp.lang.ruby Organization: Customer of PlusNet plc (http://www.plus.net) NNTP-Posting-Host: d8c6eb6d.ptn-nntp-reader02.plus.net X-Trace: DXC=JRPn>K63eHXjALJ01n[V X-Complaints-To: abuse@plus.net X-From-Usenet: see Received: header above. X-rubymirror: yes 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=-5.8 required=7.0 tests=AWL,BAYES_00, CONTENT_TYPE_PRESENT,MAILTO_TO_REMOVE,MSGID_FROM_MTA_HEADER, RCVDFRMLOCALIP autolearn=ham version=3.0.3 Mime-Version: 1.0 Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 Content-Transfer-Encoding: 8bit Precedence: bulk Lines: 49 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 On Thu, 15 Dec 2005 10:22:47 -0000, bmgz wrote: > I am a new Ruby recruit (thanks to *ahem*, Rails).. > What is the best way to do this: > > * obj.'string'+var as a method? > > I am currently using: > > * obj.send('string'+var) > > ..which works fine, but is this correct or bad practice? I'm not sure what's the best way, but here are a few things you could try: # A hash to play with h = Hash.new => {} irb(main):112:0> suff = "ore" => "ore" # You can use 'eval' but this is pretty heavy on time and space: irb(main):113:0> eval("h.st#{suff}('k','v')") => "v" irb(main):114:0> h => {"k"=>"v"} # You can use send, as you did. I'd replace 'str'+var with "str#{var}", but it's not required: irb(main):115:0> h.send("st#{suff}", 'k', 'g') => "g" irb(main):117:0> h => {"k"=>"g"} # You can get the method and call it as a proc irb(main):119:0> store = h.method("st#{suff}") => # irb(main):121:0> store.call('k','g') => "g" irb(main):122:0> store['k','g'] # (shortcut, maybe a bit confusing here though ;)) => "g" Hope that helps, Ross -- Ross Bamford - rosco@roscopeco.remove.co.uk "\e[1;31mL"