From: "David A. Black" Date: 2009-03-05T06:19:45+09:00 Subject: Re: Any solutions to Ruby's inconsistencies? Hi -- C. Dagnon wrote: > Ruby has definite positives over Java and even other scripting > languages, but many negatives too. One negative is the inconsistency of > how rules seem to be applied in the language and accompanying toolsets. > Here are a few that have bitten me: > > > 1. I almost immediately got started with Ruby on Rails and loved the > idea of partials, but then I found that the ||= idiom doesn't work on > partials' parameters. > > <% my_param_a ||= 1 # Errors out > my_param_a = my_param_a ? my_param_a : 1 # works > -%> > > That seemed odd as I thought both were equivalent, and it broke some of > the trust I had for Ruby. Not as much as early Rails' use of 2 separate > variables for RAILS_ENV, but... I can't duplicate that problem. What error message are you getting? > 3. Originally I was taught that symbols were the salvation of memory and > key problems - and they certainly sounded like it. Up until the point a > few days later when one (Rails?) map used Symbols and another used > Strings. Ouch. > Without any explicit typing abilities in the language (optional or > otherwise?) it seems like the only solution is to return to basic string > matches: > regex = Regexp.compile("^#{matcher.to_s}$", Regexp::IGNORECASE) You can use some shortcuts there: /^#{matcher}$/i > key = map.keys.find{ |key| key.to_s =~ regex } > Yuck. In 1.9, symbols can be matched against regular expressions. So you can do: [:a,:b,:c,:cat].grep(/a/) I have mixed emotions about the string-like behavior of symbols in 1.9 (they have methods like #upcase and #succ), but it has the practical advantage of making it unnecessary to worry about which you've got if you're matching patterns. > Please let me know how I can supplement my possibly deficient Ruby > education :) Relevant discussions or sections of "The Ruby Way" > appreciated! There's a book or two due out soon... see below :-) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) Ruby Training Atlanta! April 1-3, http://www.entp.com/training/atlanta09