# IF me like Ruby does

**URL:** <https://www.ruby-forum.com/t/if-me-like-ruby-does/125612>\
**Category:** Ruby\
**Created:** [January 10, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612 "2008-01-10T17:06:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 10, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612/1 "2008-01-10T17:06:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 10, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612/2 "2008-01-10T17:44:33Z")

</div>

On Jan 10, 2008 2:21 PM, Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 10, 2008, 5:21pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612/3 "2008-01-10T17:21:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 10, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612/4 "2008-01-10T19:18:33Z")

</div>

On Jan 10, 2008 11:43 AM, Bira [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 10, 2008 2:21 PM, Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [January 10, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/if-me-like-ruby-does/125612/5 "2008-01-10T23:04:51Z")

</div>

On Jan 10, 2008 4:17 PM, Austin Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 10, 2008 11:43 AM, Bira [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > How about “A#{add\_B? ’ B’ :‘’}”?
> 
> “A#{“B” if add\_B}”

Yeah, what he said :).
