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

**URL:** <https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926>\
**Category:** ruby-talk\
**Created:** [21 January 2008 10:09 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926 "2008-01-21T10:09:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Sun](https://rubytalk.org/u/Sun)\
**Post date:** [21 January 2008 10:09 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/1 "2008-01-21T10:09:57Z")

</div>

I came cross a ruby class with function defined as:

class A  
...other functions...  
def [](var1)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....  
&nbsp;&nbsp;end  
end

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [21 January 2008 10:45 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/2 "2008-01-21T10:45:58Z")

</div>

It allows you to define the operator:

irb(main):001:0\> class Foo  
irb(main):002:1\> def (v) "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

> **···**
>
> 2008/1/21, Sun \<as@hut.at\>:
> 
> > I came cross a ruby class with function defined as:
> > 
> > class A  
> > ...other functions...  
> > def (var1)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....  
> > &nbsp;&nbsp;end  
> > end
> > 
> > I do not know what this function is for and how to use it. any tips?
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Sun](https://rubytalk.org/u/Sun)\
**Post date:** [21 January 2008 11:39 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/3 "2008-01-21T11:39:56Z")

</div>

Thanks for your quick and prompt answer!  
got it.

greetings,  
Sun

"Robert Klemme" \<shortcutter@googlemail.com\> wrote in message  
news:9e3fd2c80801210246j2c4690d7i9f280cdb7ceef260@mail.gmail.com...

> **···**
>
> > 2008/1/21, Sun \<as@hut.at\>:
> > 
> > > I came cross a ruby class with function defined as:
> > > 
> > > class A  
> > > ...other functions...  
> > > def (var1)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > 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 (v) "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
> > 
> > --  
> > use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![RHS](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@RHS](https://rubytalk.org/u/RHS)\
**Post date:** [21 January 2008 14:34 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/4 "2008-01-21T14:34:57Z")

</div>

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

> **···**
>
> On Jan 21, 6:38 am, "Sun" \<a...@hut.at\> wrote:
> 
> > Thanks for your quick and prompt answer!  
> > got it.
> > 
> > greetings,  
> > Sun
> > 
> > "Robert Klemme" \<shortcut...@googlemail.com\> wrote in message
> > 
> > news:9e3fd2c80801210246j2c4690d7i9f280cdb7ceef260@mail.gmail.com...
> > 
> > \> 2008/1/21, Sun \<a...@hut.at\>:  
> > \>\> I came cross a ruby class with function defined as:
> > 
> > \>\> class A  
> > \>\> ...other functions...  
> > \>\> def (var1)  
> > \>\> ...  
> > \>\> ....  
> > \>\> end  
> > \>\> end
> > 
> > \>\> 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 (v) "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
> > 
> > \> --  
> > \> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@Sun](https://rubytalk.org/u/Sun)\
**Post date:** [21 January 2008 14:55 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/5 "2008-01-21T14:55:00Z")

</div>

> 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

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:** ![Eric\_Mahurin](https://avatars.discourse-cdn.com/v4/letter/e/b4bc9f/32.png) [@Eric\_Mahurin](https://rubytalk.org/u/Eric_Mahurin)\
**Post date:** [21 January 2008 15:38 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/6 "2008-01-21T15:38:56Z")

</div>

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.

> **···**
>
> On Jan 21, 2008 8:34 AM, RHS \<RHSeeger@gmail.com\> wrote:
> 
> > 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?

---

<div class="post-metadata">

**Author:** ![Yossef\_Mendelssohn](https://avatars.discourse-cdn.com/v4/letter/y/f19dbf/32.png) [@Yossef\_Mendelssohn](https://rubytalk.org/u/Yossef_Mendelssohn)\
**Post date:** [21 January 2008 15:40 UTC](https://rubytalk.org/t/what-does-def-var1-means-and-how-to-use/43926/7 "2008-01-21T15:40:07Z")

</div>

> 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

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'  
&nbsp;&nbsp;from (irb):5:in `method'  
&nbsp;&nbsp;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, 8:34 am, RHS \<RHSee...@gmail.com\> wrote:
> 
> --  
> -yossef
