question about a particular routing

G

gerberdata

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
 
R

Robert Klemme

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
 
M

Mike Austin

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.

Just want to add that [] can be a bad choice if this is ever used with other classes that define their own [] with other semantics, such as Array (theslice operator). Also, the parameter "method" could probably be named "method_name" to make it less confusing (to equate it with something that sounds like a string.
Oh, and btw "self." is superfluous.

But doesn't hurt. It makes it clear that you are acting upon self, not some method in an outer scope.

Mike
 
R

Robert Klemme

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.
Just want to add that [] can be a bad choice if this is ever used
with other classes that define their own [] with other semantics,
such as Array (the slice operator). Also, the parameter "method"
could probably be named "method_name" to make it less confusing (to
equate it withsomething that sounds like a string.
Right.
Oh, and btw "self." is superfluous.

But doesn't hurt. It makes it clear that you are acting upon self,
not some method in an outer scope.

It can hurt:

$ ruby19 x.rb
inner
x.rb:6:in `baz': private method `foo' called for #<X:0x2019cc2c>
(NoMethodError)
from x.rb:13:in `<main>'

$ cat -n x.rb
1
2 def foo; puts "outer" end
3
4 class X
5 def bar; foo; end
6 def baz; self.foo; end
7 private
8 def foo; puts "inner" end
9 end
10
11 x = X.new
12 x.bar
13 x.baz
14

I wouldn't make it a habit to use "send." unless really needed (e.g. for
setter invocation). It's clear that __send__(method.to_sym) invokes a
method on self anyway.

Kind regards

robert
 
G

gerberdata

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.
Just want to add that [] can be a bad choice if this is ever used
with other classes that define their own [] with other semantics,
such as Array (the slice operator). Also, the parameter "method"
could probably be named "method_name" to make it less confusing (to
equate it withsomething that sounds like a string.
Right.
Oh, and btw "self." is superfluous.
But doesn't hurt. It makes it clear that you are acting upon self,
not  some method in an outer scope.

It can hurt:

$ ruby19 x.rb
inner
x.rb:6:in `baz': private method `foo' called for #<X:0x2019cc2c>
(NoMethodError)
         from x.rb:13:in `<main>'

$ cat -n x.rb
      1
      2  def foo; puts "outer" end
      3
      4  class X
      5    def bar; foo; end
      6    def baz; self.foo; end
      7  private
      8    def foo; puts "inner" end
      9  end
     10
     11  x = X.new
     12  x.bar
     13  x.baz
     14

I wouldn't make it a habit to use "send." unless really needed (e.g. for
setter invocation).  It's clear that __send__(method.to_sym) invokes a
method on self anyway.

Kind regards

        robert

So I found this in the Twitter gem
https://github.com/jnunemaker/twitter/blob/master/lib/twitter/base.rb
and I am still unclear as to what is the alternative to this as it is
currently written.
 
M

Mike Austin

It can hurt:

$ ruby19 x.rb
inner
x.rb:6:in `baz': private method `foo' called for #<X:0x2019cc2c>
(NoMethodError)
from x.rb:13:in `<main>'

$ cat -n x.rb
1
2 def foo; puts "outer" end
3
4 class X
5 def bar; foo; end
6 def baz; self.foo; end
7 private
8 def foo; puts "inner" end
9 end
10
11 x = X.new
12 x.bar
13 x.baz
14

You got me :) I'm used to Objective-C these days, and forgot that self refers the that specific class instance in this situation.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top