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

**URL:** <https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556>\
**Category:** Rails\
**Created:** [December 6, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556 "2006-12-06T20:58:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556/1 "2006-12-06T20:58:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556/2 "2006-12-06T21:14:08Z")

</div>

Hello Jason,

2006/12/6, Jason Vogel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> \<% 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://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556/3 "2006-12-06T21:20:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556/4 "2006-12-06T22:06:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [December 6, 2006, 10:34pm UTC](https://www.ruby-forum.com/t/clean-way-to-inject-raw-text-from-within-a-block/78556/5 "2006-12-06T22:34:32Z")

</div>

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.
