0

how can I create _[ args ]() method in one class?

like said here How does defining [square bracket] method in Ruby work?

a[b] is the same as a.[](b)
a[b] = c is the same as a.[]=(b, c)

I want to do the same thing but with my special characters, like :

my_instance_['foo'] have to be same as my_instance._[]('foo')
my_instance_[:bar] = :baz  have to be same as my_instance._[]=(:bar, :baz)

"_[]" special named method with no "()" needed, and no "." point too, when call it.

How writte declaration of this method in ruby?

thx.

Community
  • 1
  • 1
Matrix
  • 3,458
  • 6
  • 40
  • 76
  • `[['foo']]` is just passing `['foo']` to the `[]` method. – August Dec 27 '14 at 21:52
  • @August ok, change special character so : [[]] became _[] for exemple. I have EDIT my question. – Matrix Dec 27 '14 at 21:55
  • defining a method named `_[]` is not possible. It would be ambiguous in any case. – August Dec 27 '14 at 21:57
  • If you're trying to define a new operator called `_[]`, that is impossible. You cannot define new operators in Ruby (or the vast majority of other languages). – user229044 Dec 27 '14 at 22:03
  • @Matrix for what reason you want to do something very weird? maybe you want to learn some Perl magic? – gaussblurinc Dec 27 '14 at 22:06
  • @gaussblurinc I want use magic access to my method, to implement my own array system. – Matrix Dec 28 '14 at 01:37
  • @Matrix You can't do that, it's impossible, as I stated. – user229044 Dec 28 '14 at 02:13
  • @Matrix, maybe you want implement [Iterator design pattern](http://en.wikipedia.org/wiki/Iterator_pattern)? It is really better to understand this pattern rather than try to invent new syntax sugar. – gaussblurinc Dec 28 '14 at 09:50

1 Answers1

1

If you want instances of your class to be able to use [], then you define a method on the class called []:

class Test
  def [](index)
    puts index
  end
end

Test.new['3'] # writes '3'

In your example, you would use

my_instance_ = Test.new
my_instance_[3]
user229044
  • 232,980
  • 40
  • 330
  • 338
  • @Matrix What you want to do is *impossible*. You cannot use `_[]`, it cannot work. Writing `my_instance_[]` will **always** be parsed as accessing a method or local variable called `my_instance_` and invoking `[]` on the return value. – user229044 Dec 28 '14 at 02:12
  • ... You want `_||` to be a method? Or you want to be able to index things using `my_instance|3|`? Neither option can work. Again, you **cannot invent operators**. That would involve introducing changes to the way that Ruby is parsed. You **can't do that**. You can only change the behaviors of existing operators. If you want an "indexed" operator, your *only* options are `[]` and `()`. – user229044 Dec 28 '14 at 02:30
  • `my_instance|3|` yes. In fact, I don't understand why [] exception works and why we can't add more exceptions. It will be interesting to let control to make own "magic method". – Matrix Dec 28 '14 at 02:34
  • 1
    Because matz says so. – Jörg W Mittag Dec 28 '14 at 02:40
  • 2
    @Matrix Because that's the grammar of the language. Ruby makes specific allowances for you to define your own `[]` and `()` behaviors, and **no others**. It doesn't allow you to define your own operators, it allows you to *change* existing operators so they have meaning for your classes. There is no "magic method", so stop looking for one. – user229044 Dec 28 '14 at 02:45