Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: question about a particular routing Date: Tue, 21 Feb 2012 23:02:37 +0100 Lines: 24 Message-ID: <9qiijuF3qsU1@mid.individual.net> References: <2e41eaf6-ff9d-4900-be9b-45e3a5ed5def@t5g2000yqk.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net kf94kOrhAd62YWIB9IdyJQm9OzFTEyDFDeGLo+aXE5cHQx4Uc= Cancel-Lock: sha1:RE4VamuYLQOKcfLySs7C2/s7vtU= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 In-Reply-To: <2e41eaf6-ff9d-4900-be9b-45e3a5ed5def@t5g2000yqk.googlegroups.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:6465 On 21.02.2012 21:30, gerberdata wrote: > I found this method definition and I was wondering if someone can > explain it to me or how to use it. > > def [](method) > self.__send__(method.to_sym) > rescue NoMethodError > nil > end That method allows for conveniently invoking methods by name without throwing an error if the method does not exist. You could invoke method "foo" by doing obj["foo"] and do not need to worry for NoMethodError - you'll just get nil back. Oh, and btw "self." is superfluous. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/