4 ms·
WOW I had no idea Ruby was so succinct compared to Perl! Could you please share a sample of code that takes 20 times more lines in Perl than Ruby.
by patrickas 17y ago
WOW I had no idea Ruby was so succinct compared to Perl!
Could you please share a sample of code that takes 20 times more lines in Perl than Ruby.
- tjic 17y agoI've got an admin interface to an ecommerce website. The admin screen gives access to all sorts of entities: series of videos, individual videos, copies of DVDs, customers, wiki pages, etc. Let's say that there are 40 different types of entities. I could write custom views for each of these. Or, using Ruby's reflection, I had them all share common views which then use reflection to figure out the basic data and the relationships between data types. E.g.: <% reflections = item.class.reflections.select {|key, val| [ :has_one, :has_many, :belongs_to, :has_and_belongs_to_many ].include?(val.macro) }.to_array reflections.each do |key,val| if ! reject.include?(key) %> <h1> <%= key.to_s %></h1> <% bullet_pts = item.send(key).to_array if bullet_pts.empty? %> <span style="padding-left:25px;"><i>none</i></span> <% else %> <ul> <% bullet_pts.sort_by {|x| x.methods.include?(:name) ? x.name : 0}.each do |subitem| %> <li><%= linktext = subitem.respond_to?(:name) ? subitem.name : (subitem.respond_to?(:text) ? subitem.text : subitem.id ) # TJICFIX P3: this is an ugly way to specify a link. do it better! link_to(linktext, :controller => subitem.class.to_s.pluralize.underscore, :action=>:show, :id =>subitem.id) + "<br>" %> <% end %> </ul> <% end # if "none" else "lots!" end # if ... reject end # reflections.each %> With the above as a partial, and one other that's similar, a full view for a very very complicated object becomes this: <%= render (:partial => 'admin/shared/show_basicdata', :locals => { :item => @item }) %> <%= render (:partial => 'admin/shared/show_relations', :locals => { :item => @item, :reject => [:category_courses] }) %> I'm reworking this approach for a new project right now. The new code goes beyond "index" and "show" views to "edit" and "create" views. Also, because most of the controllers are pretty similar, except for business logic, I'm also about to start compressing those down with metaprogramming (except for callbacks, which will vary). In this particular codebase, I think that the expansion to rewrite it in Perl would be much more than 20x. Maybe more like 100x ... or 200x.
- berntb 17y agoEven Java has reflection... :-) What is the Ruby-unique feature not existing in e.g. Moose? (I'm not a real web programmer these days, but is it still common to template with code PHP-style, like that?) Edit: Emphasis on my point, since I only got an answer to my humorous note that these days, most languages have reflection functionality.
- tjic 17y ago> Even Java has reflection We were debating PERL vs. Ruby. Java's irrelevant.
- kscaldef 17y agoGiven a framework like Moose (entirely fair, since you've dragged Rails into the discussion), I'm not seeing anything yet that can't be done with Perl. I sort of get the feeling you haven't used Perl in a while and aren't familiar with any of the modern object frameworks.