# Do I need self in this method definition

**URL:** <https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382>\
**Category:** Ruby\
**Created:** [June 3, 2009, 4:17am UTC](https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382 "2009-06-03T04:17:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 3, 2009, 4:17am UTC](https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382/1 "2009-06-03T04:17:03Z")

</div>

Hello everyone,

I have two hashes. They have the same keys but different or the same  
values.  
I want to loop through one hash and print out the values in both  
hashes for the same key.I add a method to class Hash. I run the script  
and it prints out the expected results. I wonder if I have to use  
self#method in my implement.  
Thanks for all the comments.

Li

class Hash  
def self.my\_each(hash1={}, hash2={})  
hash1.each do |k,v|  
if hash2.has\_key?(k)  
print k,"\t",v,"\t", hash2[k],"\n"  
else  
print k,"\t",v,"\n"  
end  
end  
end  
end

##############main#############  
hash1={  
‘ATC’=\>1,  
‘CTA’=\>2,  
‘CAT’=\>10,  
‘CCC’=\>1  
}

hash2={  
‘ATC’=\>3,  
‘CTA’=\>2,  
‘CAT’=\>11  
}

Hash.my\_each(hash1, hash2)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 3, 2009, 5:00am UTC](https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382/2 "2009-06-03T05:00:59Z")

</div>

Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and it prints out the expected results. I wonder if I have to use  
> self#method in my implement.
> 
> class Hash  
> def self.my\_each(hash1={}, hash2={})  
> end  
> end
> 
> Hash.my\_each(hash1, hash2)

You do if you really want a class method - that is, if you want to be  
able to call this thing by saying Hash.my\_each(hash1, hash2).

The alternative is to make an instance method. Then you could call it by  
saying hash1.my\_each(hash2). In that case, you would not use “self” in  
the def. m.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 3, 2009, 5:09am UTC](https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382/3 "2009-06-03T05:09:44Z")

</div>

matt neuburg wrote:

> You do if you really want a class method - that is, if you want to be  
> able to call this thing by saying Hash.my\_each(hash1, hash2).
> 
> The alternative is to make an instance method. Then you could call it by  
> saying hash1.my\_each(hash2). In that case, you would not use “self” in  
> the def. m.

Thank you so much. Here is my implement of an instance method. But I  
cannot explain very clear why I can #each directly without a receiver.

Li

class Hash  
def my\_each(hash2={})  
each do |k,v|  
if hash2.has\_key?(k)  
print k,"\t",v,"\t", hash2[k],"\n"  
else  
print k,"\t",v,"\n"  
end  
end  
end

end

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 3, 2009, 5:40am UTC](https://www.ruby-forum.com/t/do-i-need-self-in-this-method-definition/169382/4 "2009-06-03T05:40:45Z")

</div>

Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you so much. Here is my implement of an instance method. But I  
> cannot explain very clear why I can #each directly without a receiver.

Method calls are sent to “self” if they are not sent explicitly to a  
receiver. Since this is an instance method, “self” is a Hash instance.

You _can_ say “self.each” in that case if you want to. Most people  
don’t; I usually do; it’s a matter of style. However, in _some_ cases  
(e.g. the method is private) you _can’t_ say “self” (because “private”  
means that an explicit receiver is not allowed). For example, that is  
why you can say

print “hello”

but you can’t say

self.print “hello”

It’s because “print” is an instance method of the instance you are in,  
but it’s private.

m.
