Hi,
I have a hash something like {:schools_name =>
''''test", :schools_address => "test, etc....}
What i need to know is does any of the keys contain the word "schools"
in it.
So
hash.has_key? :schools_name will return true
But how can i get it to return true if part of any key contains the
word "schools"
I can think of a couple of ways using a loop but surely there has to
be a quick 1 line way?
JB
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
On 11 March 2010 11:13, johnnybutler7 <johnnybutler7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I have a hash something like {:schools_name => > ''''test", :schools_address => "test, etc....} > > What i need to know is does any of the keys contain the word "schools" > in it. > > So > > hash.has_key? :schools_name will return true > > But how can i get it to return true if part of any key contains the > word "schools" > > I can think of a couple of ways using a loop but surely there has to > be a quick 1 line way? > > JB >There''s a few things you can do with enumerable methods (http://ruby-doc.org/core/classes/Enumerable.html) depending on what you want to know from the match (just whether it''s in there somewhere; exactly where it is; all the keys that match; etc) Personally, I''d recommend looking at "select" and "detect" methods as a start, but here''s a line using a loop (like you said ;-) Run this at the console to see all of the positions of the matches shown. hash.keys.each { |hash_key| puts hash_key.to_s =~ /schools/ } But I *think* this is what you''re after, but if not, hopefully it puts you on the right track: hash.keys.select { |hash_key| hash_key.to_s =~ /schools/ } -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Pedro Fernandes Steimbruch
2010-Mar-11 11:53 UTC
Re: Determine if part of a hash key exists
h.each_key.collect {|k| k if k.to_s.include?(''school'') }
irb(main):007:0> h = {:school_name => ''RoR School'',
:school_address => ''RoR
Street''}
=> {:school_name=>"RoR School", :school_address=>"RoR
Street"}
irb(main):008:0> h.each_key.collect {|k| k if
k.to_s.include?(''school'') }
=> [:school_name, :school_address]
irb(main):009:0>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Ye this is what i had come up with
hash.keys.collect {|k| k.to_s.include?(''schools'') }
like the regular expression better though,
cheers,
JB
On 11 Mar, 11:50, Michael Pavling
<pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> On 11 March 2010 11:13, johnnybutler7
<johnnybutl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
>
>
>
>
> > Hi,
>
> > I have a hash something like {:schools_name =>
> > ''''test", :schools_address => "test,
etc....}
>
> > What i need to know is does any of the keys contain the word
"schools"
> > in it.
>
> > So
>
> > hash.has_key? :schools_name will return true
>
> > But how can i get it to return true if part of any key contains the
> > word "schools"
>
> > I can think of a couple of ways using a loop but surely there has to
> > be a quick 1 line way?
>
> > JB
>
> There''s a few things you can do with enumerable methods
> (http://ruby-doc.org/core/classes/Enumerable.html) depending on what
> you want to know from the match (just whether it''s in there
somewhere;
> exactly where it is; all the keys that match; etc)
>
> Personally, I''d recommend looking at "select" and
"detect" methods as
> a start, but here''s a line using a loop (like you said ;-)
> Run this at the console to see all of the positions of the matches shown.
>
> hash.keys.each { |hash_key| puts hash_key.to_s =~ /schools/ }
>
> But I *think* this is what you''re after, but if not, hopefully it
puts
> you on the right track:
>
> hash.keys.select { |hash_key| hash_key.to_s =~ /schools/ }
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.