Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3219
| From | Jesús Gabriel y Galán <jgabrielygalan@gmail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: Can I check contain value in hash? |
| Date | 2011-04-20 02:49 -0500 |
| Organization | Service de news de lacave.net |
| Message-ID | <BANLkTinHV63ORGKHVBwp2NwBicfUmW6Nwg@mail.gmail.com> (permalink) |
| References | <4c6b84f27beab6e6f15f2d315a4bef87@ruby-forum.com> |
On Wed, Apr 20, 2011 at 6:02 AM, Siratinee Sukachai
<ploy.sukachai@gmail.com> wrote:
> I have hash which is contain list of string value.
> I need to check is the value already contained in the key.
> Add add the value to the key if the key is not contained that value.
> Can I do that?
>
> like in c#
>
> if(!dict[key].contains(value))
> {
> dict[key].add(value);
> }
You can try the code. A similar approach to the c# above could use
methods Hash#has_key?
(http://ruby-doc.org/core-1.8.7/classes/Hash.html#M000515),
Array#include (http://ruby-doc.org/core-1.8.7/classes/Array.html#M000343)
and Array#<< (http://ruby-doc.org/core-1.8.7/classes/Array.html#M000306).
Although if your arrays can grow and so you are checking include? many
times on big arrays, you might want to use a Set instead of an Array.
Jesus.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Can I check contain value in hash? Siratinee Sukachai <ploy.sukachai@gmail.com> - 2011-04-19 23:02 -0500
Re: Can I check contain value in hash? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-20 02:49 -0500
Re: Can I check contain value in hash? Brian Candler <b.candler@pobox.com> - 2011-04-20 03:33 -0500
Re: Can I check contain value in hash? Robert Klemme <shortcutter@googlemail.com> - 2011-04-20 03:50 -0500
Re: Can I check contain value in hash? Brian Candler <b.candler@pobox.com> - 2011-04-20 10:25 -0500
Re: Can I check contain value in hash? Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 02:12 -0500
csiph-web