Evil Code: Ruby < Perl

OMG this sucks… :) But is kind of fun Ruby convolution… acts_as_creates_unreadable_code

More after the break (thanks to WordPress being wicked smart and auto correcting this entry to screw up the rest of the blog.


[source:javascript]
current_tab = < %= :availablitiy.to_s.titleize %>
function set_tab(t) {
//turn off current tabs/blocks
var f = $$(‘ul#small-tabs li’);
for(var i = 0; i < f.length; i++) { f[i].className = ""; } var f = $$('div#tab-text div'); for(var i = 0; i < f.length; i++) { f[i].style.display = "none"; } $(t).className = "tab_selected_small"; $(t + "_text").style.display = "block"; } [/source] [source:ruby] < % # this definitely needs cleanup %>

    < % tabs = [:availability, :standard_duration, :inclusions, :scheduling, :disclaimers, :notes] %>
    < % tabs.each do |t| %>
    < % if t == tabs.first %>

  • ‘);”>< %=t.to_s.titleize%>
  • < % else %>

  • ‘);”>< %=t.to_s.titleize.gsub(" "," ")%>
  • < % end %>
    < % end %>

< % tabs.each do |t| %>
< % if t == tabs.first %>

_text” style=”display:none;”>
< % end %>
< %=form.text_area(t, :cols => 70)%>

< % end %>

< % cb_tables = [:experience_categories, :experience_styles, :experience_occasions] %>

< % cb_tables.each do |t| %>

< % end %>

< % cb_tables.each do |t| %>

< % end %>

< %=t.to_s.titleize.split(" ")[1]%>
< % for f in eval(t.to_s.classify).find(:all, :order => :name) %>
< %= check_box_tag "#{t.to_s.singularize}[#{t.to_s.singularize}_ids][]", f.id, eval("@experience.#{t.to_s}").include?(f) %>
< %=f.name %>
< % end %>

[/source]

Creates this out put (which includes javascript tabs for swapping out the appropriate check box…

Picture 3

Bonus points for hideous UI design.

The $$(‘ul#small-tabs li’) stuff in prototype is super useful though… Using the $$() Function

But this is definitely a violation of something – this might be the ugliest ruby code I’ve ever written…

[source:ruby]
check_box_tag “#{t.to_s.singularize}[#{t.to_s.singularize}_ids][]”, f.id, eval(“@experience.#{t.to_s}”).include?(f)
[/source:ruby]

For a symbol “:experience_categories” this would generate code like:

[source:ruby]
check_box_tag “experience_category[experience_category_ids][]”, f.id, @experience.experience_categories.include?(f)
[/source]

Which just generates a check box.

There – that should REALLY bother Kipp and Matt …