enduro
1
Hi there
When fiddling together CSS classes in html_options of my form helpers, I
sometimes have code looking something like the following:
html_options.merge!(:class => “list_item” + ((options[:class_method])?("
list_item_#{object.send(options[:class_method])}"):(’’)))
The “(’’)” is pretty ugly, so I’m wondering if there’s a more rubyish
way to write this.
Thanks for your hints, -sven
enduro
2
On Jan 10, 2008 2:21 PM, Sven S. [email protected] wrote:
Here’s a version that’s easier to read. Assume you want to concat A and
B with a space in between provided the method add_B? returns true.
How about “A#{add_B? ’ B’ :‘’}”?
If you use double-quoted strings, you can interpolate Ruby code into
them by using #{}.
Ruby will replace the code between #{} with a string containing the
result of the expression inside.
enduro
3
Here’s a version that’s easier to read. Assume you want to concat A and
B with a space in between provided the method add_B? returns true.
‘A’ + ((add_B?)?(’ B’):(’’))
The closest alternative I came up with:
[‘A’, (’ B’ if add_B?)].join
The more generic form, however, will not do the trick:
[‘A’, (‘B’ if add_B?)].join(’ ')
However, this adds a tailing space if add_B? returns false.
enduro
4
On Jan 10, 2008 11:43 AM, Bira [email protected] wrote:
On Jan 10, 2008 2:21 PM, Sven S. [email protected] wrote:
Here’s a version that’s easier to read. Assume you want to concat A and
B with a space in between provided the method add_B? returns true.
How about “A#{add_B? ’ B’ :‘’}”?
“A#{“B” if add_B}”
-austin
enduro
5
On Jan 10, 2008 4:17 PM, Austin Z. [email protected] wrote:
On Jan 10, 2008 11:43 AM, Bira [email protected] wrote:
How about “A#{add_B? ’ B’ :‘’}”?
“A#{“B” if add_B}”
Yeah, what he said :).