Assaph Mehr
4/28/2005 2:37:00 AM
basi wrote:
> Hello,
> I need to check if a word is in a list. I'm using hash because the
> lists can be long (i'm under the impression hash is faster than
array).
> Basically I'm only interested that a key exists. I have to create
> entries like:
>
> h = {
> "one" => true,
> "two" => true,
> "three" => true,
> "four" => true,
> "five" => true
> }
>
> So h["one"] gives true, and h["seven"] returns nil. But typing the
> dummy value "true" is a waste of effort, and there's an ugliness in
> there. Is there a better way to design a simple checklist?
irb(main):001:0> require 'set'
=> true
irb(main):002:0> s = Set.new %w{one two three four}
=> #<Set: {"three", "two", "one", "four"}>
irb(main):005:0> s.member? 'one'
=> true
irb(main):006:0> s.member? 'seven'
=> false