# Code Reduction

**URL:** <https://www.ruby-forum.com/t/code-reduction/129786>\
**Category:** Ruby\
**Created:** [February 21, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/code-reduction/129786 "2008-02-21T15:56:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/1 "2008-02-21T15:56:39Z")

</div>

I am using ERB templates in my code. It seems too big, can’t get my head  
around on how to reduce it. I am sure this can be made smaller. My  
problem is that if there ain’t any flash messages than the contained  
HTML shouldn’t be rendered at all.

\<% if flash[:notice] %\>

  
  
\<%= flash[:notice] %\>  
   

  
\<% end %\>

\<% if flash[:error] %\>

  
  
\<%= flash[:error] %\>  
   

  
\<% end %\>

thanks.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/2 "2008-02-21T16:02:56Z")

</div>

Hey,

If you call your CSS class for a `notice’ flash NoticeMessage isntead of  
just Message, you can make it simple like this:

\<% flash.each do |k, v| %\>  
\<% if v %\>

  
  
\<%= v %\>  
   

  
\<% end %\>  
\<% end %\>

Cheers,  
Arlen.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/3 "2008-02-21T16:15:23Z")

</div>

Hiya,

On Fri, Feb 22, 2008 at 2:07 AM, Jigar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> The “if v” can be removed, cause if the key exists than there is no  
> problem in rendering its value.

Good point, I’m asleep!

Thanks a lot, that was a really quick reply.

No problems.

> I just love ruby, cause so much can be expressed in so less.

You’re absolutely right. 🙂 I recently read a good article to this  
effect:  
“Do not learn Ruby”  
[Do not learn Ruby « Programblings](http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/). It’s a fun  
read.

Cheers,  
Arlen.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:08pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/4 "2008-02-21T16:08:00Z")

</div>

Cool, thanks.

The “if v” can be removed, cause if the key exists than there is no  
problem in rendering its value.

Thanks a lot, that was a really quick reply.

I just love ruby, cause so much can be expressed in so less.

Arlen C. wrote:

> Hey,
> 
> If you call your CSS class for a `notice’ flash NoticeMessage isntead of  
> just Message, you can make it simple like this:
> 
> \<% flash.each do |k, v| %\>  
> \<% if v %\>
> 
>   
>   
> \<%= v %\>  
>    
> 
>   
> \<% end %\>  
> \<% end %\>
> 
> Cheers,  
> Arlen.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/5 "2008-02-21T16:21:05Z")

</div>

from: [Do not learn Ruby « Programblings](http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/)

"Ruby will get under your skin. You will miss its features and quirks  
when youâ€™re not using it. You might even find other languages  
insufferable, once you get comfortable with Ruby.

After youâ€™ve started using Ruby, thereâ€™s a significant chance youâ€™ll  
start loathing whatever code base you currently have to work on.  
Especially if itâ€™s a statically compiled language. A code base you used  
to think was ok, except for its few quirks."

thats exactly what I am experiencing now 🙂

thanks for pointing it out.

Arlen C. wrote:

> Hiya,
> 
> On Fri, Feb 22, 2008 at 2:07 AM, Jigar G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > The “if v” can be removed, cause if the key exists than there is no  
> > problem in rendering its value.
> 
> Good point, I’m asleep!
> 
> Thanks a lot, that was a really quick reply.
> 
> No problems.
> 
> > I just love ruby, cause so much can be expressed in so less.
> 
> You’re absolutely right. 🙂 I recently read a good article to this  
> effect:  
> “Do not learn Ruby”  
> [Do not learn Ruby « Programblings](http://webmat.wordpress.com/2008/02/20/do-not-learn-ruby/). It’s a fun  
> read.
> 
> Cheers,  
> Arlen.

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/6 "2008-02-21T16:30:42Z")

</div>

On Feb 21, 2008, at 10:14 AM, Arlen C. wrote:

> Thanks a lot, that was a really quick reply.  
> fun read.
> 
> Cheers,  
> Arlen.

Or put one of these in your application\_helper (not both, or combine  
them yourself) (Apologies for the inevitable email wrapping 😉

module ApplicationHelper

COLORS = Hash.new(‘#ff0000’).merge!({ :notice =\> ‘#00ff00’,  
:warning =\> ‘#ff9900’,  
:error =\> ‘#ff0000’,  
}) unless const\_defined? ‘COLORS’

# Shows the flash message(s) with a highlight effect

def flash\_div(\*keys)  
keys.collect { |key|  
content\_tag(:div, flash[key],  
:id =\> “#{key}”,  
:class =\> “flash”) if flash[key]  
}.join +  
keys.collect { |key|  
javascript\_tag(visual\_effect(:highlight, “#{key}”,  
:startcolor =\> “‘#{COLORS[key]}’”,  
:endcolor =\> “‘#ffffff’”)) if  
flash[key]  
}.join  
end

# Shows the flash message(s), but they go away when clicked. An

options hash will be applied to a surrounding div tag.  
def flash\_div(\*keys)  
return ‘’ if keys.blank?  
options = { :id =\> ‘flash’ }  
options.update(keys.pop) if keys.last.is\_a? Hash  
content\_tag(:div,  
keys.collect { |key|  
content\_tag(:div,  
flash[key], :id =\> “#{key}-  
flash”, :class =\> “#{key} section”,  
:onClick =\> “new Effect.Fade(this);”)  
if flash[key]  
}.join,  
options)  
end

end

And then in your views (or layout):

```
   <%= flash_div :notice, :warning, :error -%>

```

Then toss in some CSS to taste and serve warm.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![cujigar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/2acd7d/32.png) [@cujigar](https://www.ruby-forum.com/u/cujigar)\
**Post date:** [February 21, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/code-reduction/129786/7 "2008-02-21T16:38:36Z")

</div>

thanks this is cool too.

I don’t know why, but I don’t like generating HTML pragmatically.

Maybe the fear came from outputting HTML directly from Servlets as java  
strings. Ahaa those were the days 🙂

Rob B. wrote:

> Or put one of these in your application\_helper (not both, or combine  
> them yourself) (Apologies for the inevitable email wrapping 😉
