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

**URL:** <https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683>\
**Category:** Ruby\
**Created:** [January 21, 2008, 11:10am UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683 "2008-01-21T11:10:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 11:10am UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/1 "2008-01-21T11:10:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 11:46am UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/2 "2008-01-21T11:46:26Z")

</div>

2008/1/21, Sun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 12:40pm UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/3 "2008-01-21T12:40:23Z")

</div>

Thanks for your quick and prompt answer!  
got it.

greetings,  
Sun

“Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/4 "2008-01-21T15:35:46Z")

</div>

On Jan 21, 6:38 am, “Sun” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 2008/1/21, Sun [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> > 
> > > 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

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/5 "2008-01-21T15:56:27Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/6 "2008-01-21T16:41:18Z")

</div>

On Jan 21, 8:34 am, RHS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/49beb7/32.png) [@sun](https://www.ruby-forum.com/u/sun)\
**Post date:** [January 21, 2008, 4:39pm UTC](https://www.ruby-forum.com/t/what-does-def-var1-means-and-how-to-use/126683/7 "2008-01-21T16:39:32Z")

</div>

On Jan 21, 2008 8:34 AM, RHS [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
