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?
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
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?
to any of the alternatives that have been suggested since. It’s too
bad that it doesn’t quite do the job for him. Sven, are you sure you
absolutely can’t tolerate an options hash with a nil value in it?
Yep, I need it with form_for in Rails which doesn’t like :method => nil.
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…
It’s called Perl golf. It amazes and terrifies me how much it is still
played.
I must say that I vastly prefer Sven’s original formulation:
mymethod :this => 'green, :that => (‘blue’ if condition)
to any of the alternatives that have been suggested since. It’s too
bad that it doesn’t quite do the job for him. Sven, are you sure you
absolutely can’t tolerate an options hash with a nil value in it?
to any of the alternatives that have been suggested since. It’s too
bad that it doesn’t quite do the job for him. Sven, are you sure you
absolutely can’t tolerate an options hash with a nil value in it?
Yep, I need it with form_for in Rails which doesn’t like :method => nil.
If you can’t use the resource oriented form of form_for which lets
Rails figure out the method based on whether the resource is new or
existing, then why not handle it on the other side of the interface
def mymethod(options={})
options[:method] ||= :put
#…
end
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!
Here are two suggestions that are more verbose, but also more readable,
especially if you have many arguments or more complex logic:
def with_params
h = {}
yield h
h
end
def mymethod h
p h
end
a = 1
mymethod with_params {|h|
h[:this] = ‘green’
h[:that] = ‘blue’ if a==2
}
You can make this a little tighter, at the expense of changing scope
within the block:
class ParamHash < Hash
def method_missing(k, v, *)
self[k] = v
end
end
def with_params2(&block)
ph = ParamHash.new
ph.instance_eval(&block)
ph
end
a = 1
mymethod with_params2 {
this ‘green’
that ‘blue’ if a==2
}
Warning: because of the instance_eval, self is not useful in this block.
For example,
something 'red' if @x==1
or
something 'red' if foo()==1
won’t work as expected. Also, in this second example, unlike the first,
your keys will be limited to symbols which are not instance methods of
Object or Kernel (or you could use a blank slate).
If you can’t use the resource oriented form of form_for which lets
Rails figure out the method based on whether the resource is new or
existing, then why not handle it on the other side of the interface
You’re right. However, I was wondering if there were a solution that
would be both simple and stay in the view.
As a matter of fact, form_for figures the method out just fine and so
does semantic_form_for (from Formtastic). For admin backends, however,
I’m using the “show view” for deleting resources as well, so the method
should be :delete then. And that’s when - and only when - I’d need to
pass it.
I like Cory’s one liner, it’s an acceptable solution for the time being.
Thanks, Cory!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.