[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Weird problem with case expressions

Daniel Schierbeck

7/28/2006 11:41:00 AM

I have no problem doing this:

if foo :bar then
...
end

Yet when I do this:

case "foo"
when bar :baz then "bur"
end

I get the following syntax error:

SyntaxError: compile error
(irb):30: parse error, unexpected tSYMBEG, expecting kDO or '{' or '('
when bar :baz then "bur"
^

Is this intentional? Having the expression span multiple lines doesn't
solve it.


Daniel
14 Answers

Carlos

7/28/2006 11:58:00 AM

0

Daniel Schierbeck wrote:

> I have no problem doing this:
>
> if foo :bar then
> ...
> end
>
> Yet when I do this:
>
> case "foo"
> when bar :baz then "bur"
> end
>
> I get the following syntax error:
>
> SyntaxError: compile error
> (irb):30: parse error, unexpected tSYMBEG, expecting kDO or '{' or '('
> when bar :baz then "bur"
> ^
>
> Is this intentional? Having the expression span multiple lines doesn't
> solve it.

case/when have an "alternative syntax":

case "foo"
when bar: "bur"
end

probably this syntax is clashing with your construction.
--



Matthew Smillie

7/28/2006 12:15:00 PM

0

On Jul 28, 2006, at 12:45, Daniel Schierbeck wrote:

> I have no problem doing this:
>
> if foo :bar then
> ...
> end

Which is odd, 'cause I get this error:

c = [something]
if c :foo then
puts "blah"
end
SyntaxError: compile error
(irb):3: parse error, unexpected kTHEN, expecting kEND
from (irb):5
from :0


What would you expect an expression of the form 'c :symbol' to mean
in the first place? The only parallel I can think of is some sort of
implicit concatenation like happens with strings ("foo" "bar" =>
"foobar"), but I don't think that's a generalisable operation.

> Yet when I do this:
>
> case "foo"
> when bar :baz then "bur"
> end
>
> I get the following syntax error:
>
> SyntaxError: compile error
> (irb):30: parse error, unexpected tSYMBEG, expecting kDO or '{'
> or '('
> when bar :baz then "bur"
> ^
>
> Is this intentional? Having the expression span multiple lines
> doesn't solve it.

At this point it strikes me as intentional in the sense that it's not
a valid expression to begin with. Sorry if that's not the most
helpful answer; maybe further explanations might clear things up for me.

matthew smillie.

dblack

7/28/2006 12:42:00 PM

0

Daniel Schierbeck

7/28/2006 12:46:00 PM

0

Matthew Smillie wrote:
> What would you expect an expression of the form 'c :symbol' to mean in
> the first place?

I expect `foo :bar' to be the same as `foo(:bar)'.


Daniel

dblack

7/28/2006 12:46:00 PM

0

Matthew Smillie

7/28/2006 12:48:00 PM

0

On Jul 28, 2006, at 13:41, dblack@wobblini.net wrote:

> Hi --
>
> On Fri, 28 Jul 2006, Matthew Smillie wrote:
>
>> On Jul 28, 2006, at 12:45, Daniel Schierbeck wrote:
>>
>>> I have no problem doing this:
>>>
>>> if foo :bar then
>>> ...
>>> end
>>
>> Which is odd, 'cause I get this error:
>>
>> c = [something]
>> if c :foo then
>> puts "blah"
>> end
>> SyntaxError: compile error
>> (irb):3: parse error, unexpected kTHEN, expecting kEND
>> from (irb):5
>> from :0
>>
>>
>> What would you expect an expression of the form 'c :symbol' to
>> mean in the first place?
>
> I think in Daniel's example foo is a method, and :bar is a method
> argument.

Ah, that would explain it. I admit the method/variable ambiguity
didn't even occur to me.

matthew smillie.

Daniel Schierbeck

7/28/2006 12:48:00 PM

0

Carlos wrote:
> Daniel Schierbeck wrote:
>
>> I have no problem doing this:
>>
>> if foo :bar then
>> ...
>> end
>>
>> Yet when I do this:
>>
>> case "foo"
>> when bar :baz then "bur"
>> end
>>
>> I get the following syntax error:
>>
>> SyntaxError: compile error
>> (irb):30: parse error, unexpected tSYMBEG, expecting kDO or '{' or '('
>> when bar :baz then "bur"
>> ^
>>
>> Is this intentional? Having the expression span multiple lines doesn't
>> solve it.
>
> case/when have an "alternative syntax":
>
> case "foo"
> when bar: "bur"
> end
>
> probably this syntax is clashing with your construction.

if expressions have this syntax as well:

if foo: "bar"
elsif bar: "foo"
end


Daniel

Matthew Smillie

7/28/2006 12:57:00 PM

0

On Jul 28, 2006, at 13:50, Daniel Schierbeck wrote:

> Matthew Smillie wrote:
>> What would you expect an expression of the form 'c :symbol' to
>> mean in the first place?
>
> I expect `foo :bar' to be the same as `foo(:bar)'.

Indeed - mea culpa once again.

m.s.

Daniel Schierbeck

7/28/2006 2:39:00 PM

0

dblack@wobblini.net wrote:
> Hi --
>
> On Fri, 28 Jul 2006, Daniel Schierbeck wrote:
>
>> I have no problem doing this:
>>
>> if foo :bar then
>> ...
>> end
>>
>> Yet when I do this:
>>
>> case "foo"
>> when bar :baz then "bur"
>> end
>>
>> I get the following syntax error:
>>
>> SyntaxError: compile error
>> (irb):30: parse error, unexpected tSYMBEG, expecting kDO or '{' or '('
>> when bar :baz then "bur"
>
> I guess when binds more tightly than a parentheses-less method call.
> You could do:
>
> when bar(:baz) ...

Thank you for the response. Yes, it seems there is no way to get around
using parentheses. It's actually just an aesthetic problem; the method
in question has a question mark suffix, and I like to call such methods
without parentheses. Specifically, it was an attempt at an aesthetically
pleasing version of the code in "Symbols are your friend":

case "foo"
when of_type? :int then "integer"
when of_type? :str then "string"
end

Unfortunately, I'll have to write it like this:

case "foo"
when of_type?(:int) then "integer"
when of_type?(:str) then "string"
end

It doesn't really matter that much though -- I'm not even sure I like
that solution.


Cheers, and thanks for replying,
Daniel

Ara.T.Howard

7/28/2006 2:53:00 PM

0