Prev: Different behavior of '$,' output separator in Ruby 1.9
Next: Buy Genuine Google Adsense Account only for Rs.200/- for indian people.
From: Sven S. on 5 Mar 2010 14:25 Hi Cumbersome title, here's what I'd like to do both elegantly and without monkey patching Hash: Say, I need to pass a hash to a method: mymethod :this => 'green' Now I'd like to include a second hash member only if a condition is met: mymethod :this => 'green, :that => ('blue' if condition) This of course leaves ":that => nil" in the hash if the condition is not met - which is no good, the key should just not be present at all. I could delete_if after that, but isn't there a more elegant way? Thanks for your ideas! -- Posted via http://www.ruby-forum.com/.
From: Cory Chamblin on 5 Mar 2010 15:07 Jesús Gabriel y Galán wrote: > One liner: > > mymethod(h = {:this => 'green'} && condition ? h.merge({:that => > 'blue'}) : h) > You could also do it this way: method {:Apples => 1}.merge(condition ? {:Oranges => 2} : {}) -- Posted via http://www.ruby-forum.com/.
From: Michael Fellinger on 5 Mar 2010 15:14 On Sat, Mar 6, 2010 at 4:25 AM, Sven S. <svoop(a)delirium.ch> wrote: > Hi > > Cumbersome title, here's what I'd like to do both elegantly and without > monkey patching Hash: > > Say, I need to pass a hash to a method: > > mymethod :this => 'green' > > Now I'd like to include a second hash member only if a condition is met: > > mymethod :this => 'green, :that => ('blue' if condition) > > This of course leaves ":that => nil" in the hash if the condition is not > met - which is no good, the key should just not be present at all. I > could delete_if after that, but isn't there a more elegant way? p({this: 'green'}.merge(1 == 2 ? {that: 'blue'} : {})) {:this=>"green"} p({this: 'green'}.merge(1 == 1 ? {that: 'blue'} : {})) {:this=>"green", :that=>"blue"} > Thanks for your ideas! > -- > Posted via http://www.ruby-forum.com/. > > -- Michael Fellinger CTO, The Rubyists, LLC
From: Jesús Gabriel y Galán on 5 Mar 2010 15:00 On Fri, Mar 5, 2010 at 8:25 PM, Sven S. <svoop(a)delirium.ch> wrote: > Hi > > Cumbersome title, here's what I'd like to do both elegantly and without > monkey patching Hash: > > Say, I need to pass a hash to a method: > > mymethod :this => 'green' > > Now I'd like to include a second hash member only if a condition is met: > > mymethod :this => 'green, :that => ('blue' if condition) > > This of course leaves ":that => nil" in the hash if the condition is not > met - which is no good, the key should just not be present at all. I > could delete_if after that, but isn't there a more elegant way? I'm not sure I understand. You want too add :that => 'blue' to the hash only if condition? h = {:this => 'green'} h[:that] = 'blue' if condition mymethod h One liner: mymethod(h = {:this => 'green'} && condition ? h.merge({:that => 'blue'}) : h) A bit ugly, in my opinion. I like my first way better. Jesus.
From: Nick Brown on 5 Mar 2010 16:36
>>> mymethod(h = {:this => 'green'} && condition ? h.merge({:that => >>> 'blue'}) : h) >> >> You could also do it this way: >> >> method {:Apples => 1}.merge(condition ? {:Oranges => 2} : {}) For the love of all that is good and holy, please do not try to cram two elegant lines of code into one disgusting line. That way lies madness... ;-) -- Posted via http://www.ruby-forum.com/. |