[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Fetching objects used as hash keys

Andre Nathan

2/6/2007 7:05:00 PM

Hello

I'm using objects which are instances of a class that has 3 instance
variables as hash keys, but I'm using the value of only two of those
instance variables for the hash calculation. It's something like this:

class A
attr_reader :x, :y, :z

def initialize(x, y)
@x = x
@y = y
@z = rand
end

def hash
@x.hash ^ @y.hash
end

def eql?(other)
@x == other.x and @y == other.y
end
end

a1 = A.new('foo', 'bar') # say @z = 0.25 here
a2 = A.new('foo', 'bar') # say @z = 0.90 here

h = {}
h[a1] = true
p h[a2] #=> true

I did it this way because when I need to test the presence of an object
in the hash, the value of @z is not known (and doesn't matter). However,
once I know the object is there, I'd like to fetch it, and check the
value of @z of the object that was used as the hash key, that is, I'd
like to get "0.25" in the example above.

What I had to do was use a hash of hashes instead, so I currently I have

h = {}
x = 'foo'
h[x] = {
:y => 'bar'
:z => rand
}

So I'm guessing maybe a hash isn't the correct data structure to do what
I want here (i.e., fetch an object based on some of its attributes).
Anyone has suggestions on how I could implement that, if possible
keeping the efficiency of a hash access?

Thanks in advance,
Andre


6 Answers

gga

2/8/2007 2:34:00 AM

0

On 6 feb, 16:04, Andre Nathan <a...@digirati.com.br> wrote:
> Hello
>
> I'm using objects which are instances of a class that has 3 instance
> variables as hash keys, but I'm using the value of only two of those
> instance variables for the hash calculation. It's something like this:
>
> class A
> attr_reader :x, :y, :z
>
> def initialize(x, y)
> @x = x
> @y = y
> @z = rand
> end
>
> def hash
> @x.hash ^ @y.hash
> end
>
> def eql?(other)
> @x == other.x and @y == other.y
> end
> end
>
> a1 = A.new('foo', 'bar') # say @z = 0.25 here
> a2 = A.new('foo', 'bar') # say @z = 0.90 here
>
> h = {}
> h[a1] = true
> p h[a2] #=> true
>

Okay, your explanation does not make much sense. But... if @z is not
needed and you want it to be returned as the hash value, why is it in
the class? Make *that* the hash value, like:

h = {}
h[a1] = rand
p h[a2]
>> 0.25

should give you what you want. If you do want it in the class, then
you don't need a hash. Just doing a1.z should give you back what you
want.
If you need it in both, pass it around when you create it, like:

h = {}
[ a1, a2 ]. each { |key|
z = h[key] || rand
h[key] = z
key.z = z # make z attr_accessor, not attr_reader of
course
}


Or did I miss something?

Patrick Hurley

2/8/2007 3:06:00 PM

0

On 2/6/07, Andre Nathan <andre@digirati.com.br> wrote:
> So I'm guessing maybe a hash isn't the correct data structure to do what
> I want here (i.e., fetch an object based on some of its attributes).
> Anyone has suggestions on how I could implement that, if possible
> keeping the efficiency of a hash access?

You probably want a set.

require "set"

class A
attr_reader :x, :y, :z

def initialize(x, y, z = rand)
@x = x
@y = y
@z = z
end

def hash
@x.hash ^ @y.hash
end

def eql?(other)
@x == other.x and @y == other.y
end
end

a1 = A.new('foo', 'bar') # say @z = 0.25 here
puts "a1.z: #{a1.z}"
a2 = A.new('foo', 'bar') # say @z = 0.90 here
puts "a2.z: #{a2.z}"

s = Set.new [a1]
p s
s.add(a2)
p s
p s.include?(a2)
p s.find { |m| m.eql?(a2) }.z

# Make it nicer:
def s.[] (lookup)
self.find {|elem| elem.eql?(lookup) }
end

p s[a2].z

# Hope that helps
# pth

td

9/2/2013 5:04:00 PM

0

On Monday, September 2, 2013 2:49:07 AM UTC-4, Oscar wrote:
> On Friday, August 30, 2013, td wrote:
>
> >
>
> > My goodness me.
>
> >
>
> > Finally someone has discovered Schnabel's Beethoven sonatas on their EMI
>
> > transfers. I thought I was the only one able to enjoy these.
>
> >
>
> > Mind you, I also enjoy many other versions of these 78 RPM originals. They all
>
> > betray the same characteristics, faults and assets. Nobody has ever pointed to
>
> > a single note that is different in any way other than that it has more or less
>
> > surface noise.
>
>
>
> My goodness me.
>
>
>
> A blast from the past, here is the one and only Tom D

(the rest of the post snipped)

It's been two years now.

I have to wonder what your point is?

Never spoken to Mr. St. Laurent, OR purchased one of the recordings on his label.

It must be that you have a bee in your bonnet. (we assume, of course, that you wear one)

TD

Steve de Mena

9/3/2013 6:18:00 AM

0

On 8/30/13 3:25 PM, td wrote:

> Nobody has ever pointed to a single note that is different in any way other than that it has more or less surface noise.

There were no pitch corrections made in any other transfer?

> TD
>

Steve

td

9/6/2013 9:29:00 AM

0

On Tuesday, September 3, 2013 2:18:08 AM UTC-4, Steven de Mena wrote:
> On 8/30/13 3:25 PM, td wrote:
>
>
>
> > Nobody has ever pointed to a single note that is different in any way other than that it has more or less surface noise.
>
>
>
> There were no pitch corrections made in any other transfer?

Since the invention of the strobe and the electronic tuning devices, not to mention a pitch pipe, the vast majority of 78 RPM transfers have got this aspect right, Steve.

No, the major criticism - ignorant though it be - of the EMI transfers is that they eliminate the surface noise. It is ignorant because EMI had access to vinyl pressings made from the originals, thus reducing the noise of the music. Needless to say, the die-hard EMI bashers claim that they "hear" music in all that noise. Hmmmmmm. And they are also the ones who bought all those expensive speaker wires too.

I have no idea what happened to the OP, but he seems to have had his ears cleared out. Or, as DK would have said, a new set of ears. And now Schnabel sounds wonderful, or, shall we say, just as he has always sounded, of course, on any and all transfers of these rather pathetic examples of the "art of the 78 RPM" recording system.

This is a very tired subject, by the way. We have exhausted it.

TD



Matthew B. Tepper

9/7/2013 6:39:00 AM

0

*applause*

--
Matthew B. Tepper: WWW, science fiction, classical music, ducks!!
Read about "Proty" here: http://home.earthlink.net/~oy/...
To write to me, do for my address what Androcles did for the lion
Opinions expressed here are not necessarily those of my employers.