views:

61

answers:

2

I know I could easily write a function and put it in the application controller, but I'd rather not if there is something else that does this already. Basically I want to have something like:

>> boolean_variable?
=> true
>> boolean_variable?.yesno
=> yes
>> boolean_variable?.yesno.capitalize
=> Yes

is there something like this already in the Rails framework?

+1  A: 

No such built-in helper exists, but it's trivially easy to implement:

class TrueClass
  def yesno
    "Yes"
  end
end

class FalseClass
  def yesno
    "No"
  end
end
Jordan
Hey, that's slicker then what I would have come up with. As far as a rails app is concerned, where would the best place to put this code be? in a file under lib or something?
DJTripleThreat
A: 

Alternatively, you could also do one offs in your views such as:

<%= item.bool_field? ? 'yes' : 'no' %>

This isn't very DRY, compared to the other solution and will be difficult to localize.
Jeff Paquette
No, Jeff, you're absolutely right. I was speaking to *one* offs, and not to *many* offs, and in the case of the latter, a helper would of course be the wiser (and DRY) option, as you point out.