Unless you wanted to add that method to the Hash class, you could
easily do:
[hash1, hash2].collect{|hash| hash.reject{ |key,value| !
value}.keys.join(',')}
Which would give you an array of the new hashes. If you wanted to
assign these values to x and y:
x,y = [hash1, hash2].collect{|hash| hash.reject{ |key,value| !
value}.keys.join(',')}
On Sep 18, 12:19 pm, surge <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>
> I have a line of the form:
>
> x = hash1.reject{ |key,value| !value}.keys.join(',')
>
> That worked fine but then I needed to do the same thing for hash2. So
> I had to add another line like so:
>
> y = hash2.reject{ |key,value| !value}.keys.join(',')
>
> I'm trying go DRY but I can't think of an elegant way to do it -- I'm
> a relative beginner in ruby. A function is an obvious candidate, but
> that's probably my php thinking. Any better way to avoid the
> repetition? Some fancy loop?
>
> Thanks!
> Sergei
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---