1.9.2 syntax issues

I admit I still use 1.8.x more often than 1.9.x – and I keep running
across
little things that puzzle or annoy me.

Why is it that this statement:

value

if

block_given?

  yield

str

else

str.send(converter)

end

cannot be rewritten as:

value = block_given? ? yield str : str.send(converter)

Just curious…

Hal

On Sun, Feb 19, 2012 at 3:49 PM, Hal F. [email protected]
wrote:

block_given?

Hal

It seems to be getting parsed like this
value = block_given? ? yield(str : str.send(converter))

You can get around it with parens like this:
value = block_given? ? (yield str) : str.send(converter)

Or by using the if/then/else/end keywords
value = if block_given? then yield str else str.send(converter) end

On Sun, Feb 19, 2012 at 4:08 PM, Josh C. [email protected]
wrote:

if
cannot be rewritten as:
value = block_given? ? yield(str : str.send(converter))
What syntactic sense does (str : str.send(converter)) make in Ruby? My
first thought was that it would think str is a symbol key, but that
doesn’t seem to be a case (you can’t write a hash as {foo : ‘foo’}).

On Mon, Feb 20, 2012 at 10:26 PM, Eric C. <
[email protected]> wrote:

value
end

It seems to be getting parsed like this
value = block_given? ? yield(str : str.send(converter))

What syntactic sense does (str : str.send(converter)) make in Ruby? My
first thought was that it would think str is a symbol key, but that
doesn’t seem to be a case (you can’t write a hash as {foo : ‘foo’}).

Well, you can write it as {foo: ‘foo’}, so I suppose it could look like
you
meant to write a hash but made a mistake. More likely, this is probably
just an edge case in the parser’s abilities.

On Feb 19, 2012, at 13:49 , Hal F. wrote:

end

cannot be rewritten as:

value = block_given? ? yield str : str.send(converter)

Doesn’t look like a 1.8 vs 1.9 issue:

% ruby -vwe ‘def x str; value = block_given? ? yield str :
str.send(converter); end’
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
-e:1: syntax error, unexpected tIDENTIFIER
def x str; value = block_given? ? yield str : str.send(converter); end
^

All versions of ruby that I test against want yield(str).

Ruby is icky when it comes to parentheses sometimes. It also won’t
parse “1 + f 2”, and why, I still have no idea. (I’ve been told it’s
got something to do with operator priority or correctly parsing “f
1+2”…)

It usually makes sense to imagine open-paren right after function
name, and close-paren always at the end of line or at higher-level
close-paren. (Eg. ary[func 5] - the brackets here are what I call
higher-level.)

– Matma R.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs