What does 'def [](var1)' means and how to use?

I came cross a ruby class with function defined as:

class A
…other functions…
def


end
end

I do not know what this function is for and how to use it. any tips?

2008/1/21, Sun [email protected]:

I do not know what this function is for and how to use it. any tips?
It allows you to define the [] operator:

irb(main):001:0> class Foo
irb(main):002:1> def “foo<#{v}>” end
irb(main):003:1> end
=> nil
irb(main):004:0> Foo.new[123]
=> “foo<123>”

Typically this is used for index based lookups (like Array and Hash
do) but you are free to define your own semantic. There is also the
assignment operator []= which can be defined in a similar way (but
with two arguments).

Kind regards

robert

Thanks for your quick and prompt answer!
got it.

greetings,
Sun

“Robert K.” [email protected] wrote in message
news:[email protected]

On Jan 21, 6:38 am, “Sun” [email protected] wrote:

2008/1/21, Sun [email protected]:

I do not know what this function is for and how to use it. any tips?
Typically this is used for index based lookups (like Array and Hash
do) but you are free to define your own semantic. There is also the
assignment operator []= which can be defined in a similar way (but
with two arguments).

Kind regards

robert


use.inject do |as, often| as.you_can - without end

Out of curiosity… operator overloading and it’s ilk are (by many)
considered to be bad things because it can make it hard to figure out
what’s happening in the code, especially for dynamic languages where
they can change on the fly. The reason for this thought is, I believe,
because it can be difficult to understand what code is doing if the
definitions of “standard” operations are or where they come from.

Along those lines, is there a current way to ask Ruby where the
definition comes from for a given operator or, more generally, method?
Ie, Ruby needs to do a lookup to find out what definition to use. Is
there a way to ask it to do that lookup and report the results?

Thanks,
Rob Seeger

Ie, Ruby needs to do a lookup to find out what definition to use. Is
there a way to ask it to do that lookup and report the results?

Thanks,
Rob Seeger

Have no idea of your question. I am just working with Ruby on rails
temorarily (few weeks), on a project developed by another guy who is a
big
Ruby fan. I just figured out that Ruby maybe too flexible for me, as
Perl,
and I will go back to java/python/c++ genre soon after this work done.
But
ROR is truely amazing when dealing with Web apps, quite a fun of using
it.

On Jan 21, 8:34 am, RHS [email protected] wrote:

there a way to ask it to do that lookup and report the results?

Thanks,
Rob Seeger

In fact you can, Rob. It’s the ‘method’ method. Here’s an IRB dump
with some explanations

[].method(:[])
=> #<Method: Array#[]>

[] (an Array) gets the method [] from Array

class Blah; end
=> nil
Blah.new.method(:==)
=> #<Method: Blah(Kernel)#==>

Blah.new (an instance of class Blah) has the == method from the

Kernel module (Object is the base class of all Objects and it has
Kernel mixed in)

Blah.new.method(:<=)
NameError: undefined method <=' for class Blah’
from (irb):5:in `method’
from (irb):5

Blah.new (an instance of class Blah) has no <= method

class Blah; include Comparable; end
=> Blah
Blah.new.method(:==)
=> #<Method: Blah(Comparable)#==>

mixing in the Comparable module gives some of its own definitions

for a few methods, like ==. That’s where Blah’s == comes from now, not
Kernel.

Blah.new.method(:<=)
=> #<Method: Blah(Comparable)#<=>

and now Blah also has a <= method thanks to Comparable

class Blah; def <=(other); end; end
=> nil
Blah.new.method(:<=)
=> #<Method: Blah#<=>

and now Blah has it’s own <= method defined

That’ll tell you where the method comes from. What you do with it is
up to you.

On Jan 21, 2008 8:34 AM, RHS [email protected] wrote:

there a way to ask it to do that lookup and report the results?
If you are talking about C++ or Python, I would somewhat agree. In
those
languages, the operator definition can be defined several places
depending
on the types of the operands. In C++, they can be defined in the class
for
the left-hand operand, as a regular function, and maybe other places.
In
python, you can define operators in either left-hand operand class or
the
right-hand operand class (using “reverse” operator methods).

Ruby on the other hand treats operators simply as a different syntax for
method calls. The definition of an operation will always be found in
the
class of the first operand. The first operand becomes “self” and the
others
are arguments to the method. The method name for operator is usually
the
operator itself (unary operators get an “@” to distinguish between
binary
operators).

Ruby operator overloading is not evil and not any different than normal
methods (other than some syntax). This feature is great for DSL’s and
can
give a more natural syntax to the usage of some classes. I wouldn’t
want to
deal with complex numbers, vectors, matrices, strings, arrays, etc.
without
operator overloading.

Also, without operator overloading, Ruby would not have been as
consistent
(everything is an object and all functionality comes from methods on
objects) without removing operators altogether. Compare this to Java
where
only the primitive (non-object) types can use operators.

Don’t let the “by many” (i.e. Java folks) dictate what are dangerous
language features to use. Especially when that judgment was based on a
language you are not using (C++).

p.s. In C++/python, if you limit yourself to only defining operators in
the
class of the first operand (like Ruby), operator overloading becomes
more
understandable in those languages (just like other methods). It’s all
the
extra options that make thing confusing.