From: Eric Christopherson Date: 2012-04-16T14:02:29+09:00 Subject: Re: Operator overloading of the subscript operator 2012/4/15 Bartosz Dziewoński : > W dniu 15 kwietnia 2012 18:41 użytkownik Eric Christopherson > napisał: >> On Sat, Apr 14, 2012 at 10:48 AM, Ralph Shnelvar wrote: >>> I have the Pickax book and I see two places operator overloading is mentioned: pages 130 and 345. >>> >>> Isn't there official documentation about the Ruby core? >>> >>> >>> If one looks at page 333 which lists all the operators that are expressed as methods, one sees that subscript is the only one where the expression goes lexically inside the operator.  As a special >>> case shouldn't this be documented somewhere official? >> >> There's also Kernel#`, which is called when you put something inside >> backticks or %x{}. Unlike [] and []=, though, trying to override it in >> your own classes doesn't have any effect. > > Uh, yes it does? > > irb(main):001:0> module A; def ` a; puts "backticks: #{a}"; end end > => nil > irb(main):002:0> class B; include A; def asd; `ls`; end end > => nil > irb(main):003:0> B.new.asd > backticks: ls > => nil > irb(main):004:0> include A > => Object > irb(main):005:0> `ls` > backticks: ls > => nil Oh, I see. Still, Ruby doesn't seem to convert foo`bar` into foo.`(bar) the way it converts foo[bar] into foo.[](bar): class Foo def `(x) puts "backticks around #{x}" end end f = Foo.new f`bar` # result: Errno::ENOENT: No such file or directory - bar