J
Just Another Victim of the Ambient Morality
I have some problems that I'm sure must be routine for Ruby developers
but I'm a little stumped on how to solve them. Any help is greatly
appreciated!
My first problem is that I have a class and I want to define some
operators, like equality. It's a simple class and its operators are equally
simple. Equality is simple the equality of the member variables of my
class. So, I have code that looks something like this:
class Object
attr_accessor :first, :second, :third
def initialize (first, second, third)
@first, @second, @third = first, second, third
end
def == (right) # should this be Eql?
bool = true
[:first, :second, :third].each do |member|
bool = (bool and (method(member).call ==
right.method(member).call))
end
bool
end
end
What I'm trying to do is automate iterating over my member variables
performing a cumulative equality test. The best I was able to come up with
was a single list of members but there must be some way, through inspection,
to programmatically iterator over them to do this test. Also, it looks like
there might be something a little more efficient than calling "method" on
each member and then calling "call" on each method object returned. Is
there?
My second problem is that I would like to put this simple object in a
Set (as broken as it may be). How do I do this? I know this doesn't
magically just work. I have to define something like a "hash" method but I
don't know what hash is supposed to return, exactly, for things like Set and
Hash to work. Can someone explain this to me?
Thank you for your responses! I will use them to be a better Ruby
programmer...
but I'm a little stumped on how to solve them. Any help is greatly
appreciated!
My first problem is that I have a class and I want to define some
operators, like equality. It's a simple class and its operators are equally
simple. Equality is simple the equality of the member variables of my
class. So, I have code that looks something like this:
class Object
attr_accessor :first, :second, :third
def initialize (first, second, third)
@first, @second, @third = first, second, third
end
def == (right) # should this be Eql?
bool = true
[:first, :second, :third].each do |member|
bool = (bool and (method(member).call ==
right.method(member).call))
end
bool
end
end
What I'm trying to do is automate iterating over my member variables
performing a cumulative equality test. The best I was able to come up with
was a single list of members but there must be some way, through inspection,
to programmatically iterator over them to do this test. Also, it looks like
there might be something a little more efficient than calling "method" on
each member and then calling "call" on each method object returned. Is
there?
My second problem is that I would like to put this simple object in a
Set (as broken as it may be). How do I do this? I know this doesn't
magically just work. I have to define something like a "hash" method but I
don't know what hash is supposed to return, exactly, for things like Set and
Hash to work. Can someone explain this to me?
Thank you for your responses! I will use them to be a better Ruby
programmer...