Quantcast
Channel: Planet Apache
Viewing all articles
Browse latest Browse all 9364

Rodent of Unusual Size (Ken Coar): Ruby and funky method names

$
0
0

I found out about this by accident. (Well, I went looking for a way to use a Greek letter for a method name in order to match the maths I was patterning.)

Ruby's #alias_method and #remove_method are more catholic than the def statement. With them, you can create method names that aren't ordinarily allowed:

 class Foo
 def delta
    return 'In delta'
 end
 alias_method(:'δ', :delta)
 end
 
 x = Foo.new
 x.delta
 => "In delta"
 x.send(:'δ')
 => "In delta"
 Foo.send(:remove_method, :delta)
 x.delta
 NoMethodError:
    undefined method `delta' for #«Foo:0xb76ce17c»
 from (irb):11
 x.send(:'δ')
 => "In delta"
 

It's not necessarily very useful, but it's another byway in Ruby I had fun discovering and exploring.


Viewing all articles
Browse latest Browse all 9364

Latest Images

Trending Articles



Latest Images