Clean way to "inject" raw text from within a <%%> block?

Source :
<% case list_type.upcase
when “BASE” then %> Base Coverages
<% when “OPTIONAL” then %> Optional Coverages
<% else “asdfadasdF” %> Bogus
<% end %>

The above just looks sloppy to me…

I would have hoped for something like…

<% case list_type.upcase
when “BASE” then inject(“Base Coverages”)
when “OPTIONAL” then inject(“Optional Coverages”)
else “asdfadasdF” then inject(“Bogus”)
end %>

I hate matching up the <%… 's. It is just to prone to error.

Jason

Hello Jason,

2006/12/6, Jason Vogel [email protected]:

<% case list_type.upcase
when “BASE” then %> Base Coverages
<% when “OPTIONAL” then %> Optional Coverages
<% else “asdfadasdF” %> Bogus
<% end %>

case returns it’s value, so you can do it like this:

<%= case list_type.upcase
when ‘BASE’; “Base Coverages”
when ‘OPTIONAL’; “Optional Coverages”
else; "whatever
end %>

Hope that helps !

François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

Hello Jason,

<% case list_type.upcase
when “BASE” then inject(“Base Coverages”)
when “OPTIONAL” then inject(“Optional Coverages”)
else “asdfadasdF” then inject(“Bogus”)
end %>

you’re not far :

<%= case list_type.upcase
when ‘BASE’ : ‘Base Coverages’ # different style
when “OPTIONAL” then “Optional Coverages”
else “asdfadasdF” then “Bogus”
end %>

You can also use an Hash with “Bogus” as default value.

-- Jean-François.


À la renverse.

Both of these are great. Is there a general technique to this? If I
had done with an “If…Elsif…” block, what would it have looked like?
And is there a way to “inject” text?

Thanks,
Jason

Jason :

Both of these are great. Is there a general technique to this? If I
had done with an “If…Elsif…” block, what would it have looked like?

<%= if list_type.upcase ==‘BASE’
‘Base Coverages’
else
‘Bogus’ %>

Or using ? :
<%= list_type.upcase == ‘BASE’ ? ‘Base Coverages’ : ‘Bogus’ %>

Note that in Ruby code, you can do assignments like that :

foo = if something == something_else
‘bar’
else
‘baz’
end

but ? : will be more often used.
And you can use case/when as well

foo = case …
when …
when …
end

And is there a way to “inject” text?

yes : _erbout.concat(‘foo’)

don’t forget also, that helpers can make your ERb code cleaner.

– Jean-François.


À la renverse.