Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Do I need self in this method definition

Reply
Thread Tools

Do I need self in this method definition

 
 
Li Chen
Guest
Posts: n/a
 
      06-03-2009
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)
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
matt neuburg
Guest
Posts: n/a
 
      06-03-2009
Li Chen <(E-Mail Removed)> 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.

--
matt neuburg, phd = http://www.velocityreviews.com/forums/(E-Mail Removed), http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
 
Li Chen
Guest
Posts: n/a
 
      06-03-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
matt neuburg
Guest
Posts: n/a
 
      06-03-2009
Li Chen <(E-Mail Removed)> 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.

--
matt neuburg, phd = (E-Mail Removed), http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"def self.method" vs "class << self; def method" joevandyk@gmail.com Ruby 7 10-10-2006 08:46 AM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) falcon Python 0 07-31-2005 05:41 PM
Re: __autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 2 07-12-2005 03:20 AM
Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code Ralf W. Grosse-Kunstleve Python 16 07-11-2005 09:28 PM
__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y;self.z=z boilerplate code) Ralf W. Grosse-Kunstleve Python 18 07-11-2005 04:01 PM



Advertisments