From: Ken Bloom Date: 2009-03-05T02:38:49+09:00 Subject: Re: Any solutions to Ruby's inconsistencies? On Wed, 04 Mar 2009 11:02:36 -0500, 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 don't use rails, and I don't know what a partial is, so I can't answer. You probably better ask on a Rails list. > 2. Then when using blocks to process an array I tried to use another > Ruby control structure: > > arr=[1,2,3,4] > arr2 = arr.collect{|ii| return 0 if ii == 3; ii+1} # result: > LocalJumpError: unexpected return > > That also caught me off guard - I need the block to return a value, to > evaluate to a value - but suddenly I'm told that 'return' doesn't mean > what I think it means? At least not inside a block (or Proc.new)... A block (except when it's a lambda) is meant to be looked at as though it's a control structure in the function, the same as if it were an ordinary loop. So the idea of `return` is to return from the function. Use `next` to return a value early from the block. arr2 = arr.collect{|ii| next 0 if ii == 3; ii+1} > 4. And what's up with Ruby incorrectly naming their Map 'Hash'? Or is > my C.S. idea of a Hash too limited? Granted in Java they have HashMap, > but if you enter one value and get a second one out it is a Map, > regardless of how the keys are stored. A 'Hash' would instead be a type > of set. Perhaps they should, but it would be a pain to change it now. --Ken -- Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory. Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/