Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: Set vs Hash
Peter Szinek
2/17/2007 8:47:00 AM
SunRaySon wrote:
> Are there any general guidelines on when a "Set" should be used and when a
> "Hash" should be used. Are there any limitations to "Set"
A Set is similar to an Array - the two major differences are that a Set
is unordered (because it's implementation is based on a Hash) and that a
Set contains every element only once (i.e. if you add an element which
is already there, the Set won't change).
You should use a Hash if you have a dictionary-like structure (i.e. key
=> value pairs) and a Set/Array if you have single objects. You can
think about a Set as a Hash where every value is nil - i.e. you can
represent a Set with a Hash but it makes no sense if you are not using
any values at all. In this case you should use a Set.
I hope this answers your question...
Cheers,
Peter
__
http://www.rubyra...
:: Ruby and Web2.0 blog
http://s...
:: Ruby web scraping framework
http://rubykitch...
:: The indexed archive of all things Ruby
2 Answers
Daniel Finnie
2/18/2007 4:46:00 AM
0
It should be the same as a Set is implemented as a Hash with every value
being true. Storing true and storing nil uses the same amount of memory.
Honestly, it doesn't matter. Use which ever is semantically correct; if
you need to store values with the keys then use a Hash, if you don't
then use a Set.
Dan
SunRaySon wrote:
> Thanks for your response. I think now the differences and similarities
> between Set, Hash and Array are clear to me. But with respect to memory
> consumption which one of among the following is better:
>
> s = Set.new([1,2,3])
> h = { 1=>nil, 2=>nil, 3=> nil}
>
>
> On 2/17/07, Peter Szinek <peter@rubyrailways.com> wrote:
>>
>> SunRaySon wrote:
>> > Are there any general guidelines on when a "Set" should be used and
>> when
>> a
>> > "Hash" should be used. Are there any limitations to "Set"
>>
>> A Set is similar to an Array - the two major differences are that a Set
>> is unordered (because it's implementation is based on a Hash) and that a
>> Set contains every element only once (i.e. if you add an element which
>> is already there, the Set won't change).
>>
>> You should use a Hash if you have a dictionary-like structure (i.e. key
>> => value pairs) and a Set/Array if you have single objects. You can
>> think about a Set as a Hash where every value is nil - i.e. you can
>> represent a Set with a Hash but it makes no sense if you are not using
>> any values at all. In this case you should use a Set.
>>
>> I hope this answers your question...
>>
>> Cheers,
>> Peter
>>
>> __
>>
http://www.rubyra...
:: Ruby and Web2.0 blog
>>
http://s...
:: Ruby web scraping framework
>>
http://rubykitch...
:: The indexed archive of all things Ruby
>>
>>
>>
>
Ara.T.Howard
2/18/2007 6:07:00 AM
0
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Set vs Hash
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password