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?
Thank you for your help
basi
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
Use a set:
require 'set'
words = %w(one two three four five).to_set
words.include? 'one' # => true
words.include? 'seven' # => false
http://www.ruby-doc.org/stdlib/libdoc/set/rdoc/
jeremy
Cheers!
basi
A hash have to store "key" => value pairs, how will be more efficient
than an array? and why?
> 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?
>
> Thank you for your help
irb(main):001:0> a = %w{one two three four five}
=> ["one", "two", "three", "four", "five"]
irb(main):002:0> a.member? 'one'
=> true
irb(main):003:0> a.member? 'seven'
=> false
Hope that helps.
--
Dr Balwinder Singh Dheeman Registered Linux User: #229709
CLLO (Chief Linux Learning Officer) Machines: #168573, 170593, 259192
Anu's Linux@HOME Distros: Ubuntu, Fedora, Knoppix
More: http://anu.homelinux.net/~bsd/ Visit: http://counter.li.org/
because Array lookups are O(n) and Hash lookups should be O(1). To
find something in an Array you have to traverse it completely and will
find the entry you are searching in expected n/2 steps. For hashes
there exist different implementations, but basically you compute the
possible position of an object from the object and just take a look if
it is there. (Oversimplified).
best regards,
Brian
> [snip]
--
http://ruby.brian-schroeder.de/
multilingual _non rails_ ruby based vocabulary trainer:
http://www.vocabulaire.org/ | http://www.gloser.org/ | http://www.vokabeln.net/