own method has_key for class Hash. -howto

S

salai

Dear all,

Can you help me how can I implement my own "has_key" method for "hash class."

class Hash

def my_each_key(&block)
self.each { |key|
yield(key[0])
}
end

def my_each_value(&block)
self.each { |value|
yield(value[1])
}
end

def has_key()
#todo
end

end

h = {1 => 1.0, 2 => 2.0, 3 => 3.0 }

many thanks in advance,

salai.
 
S

salai

I was able able solve the problem ...

def has_key(k)
self.each { |k|
k =3D k[0]
}
return k
end

is there any other better/easier solution?

regards,

salai.
 
S

salai

thank you!.



At said:
=A0Dear all,

=A0Can you help me how can I implement my own "has_key" method for "hash= class."

=A0class Hash
=A0 =A0 =A0 def my_each_key(&block) [...]
=A0 =A0 =A0 def my_each_value(&block) [...]
=A0 =A0 =A0 def my_has_key?(key)

=A0 =A0 =A0 =A0 =A0keys.include?(key)
=A0 =A0 =A0 end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top