[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem with case statements

hemant

12/21/2007 11:15:00 AM

I have an innocent looking code:

54: def find(*args)
55: options = args.extract_options!
56: validate_find_options(options)
57: case args.first
58: when :first: find_first(options)
59: when :all: find_all(options)
60: else raise "Invalid find"
61: end
62: end


Now, above case statement totally blows up with Ruby 1.9:

/home/hemant/push_server/lib/db_connection.rb:61: warning: else without
rescue is useless
/home/hemant/push_server/bin/boot.rb:34:in
`require': /home/hemant/push_server/lib/db_connection.rb:58: syntax
error, unexpected ':', expecting keyword_then or ',' or ';' or
'\n' (SyntaxError)
when :first: find_first(options)
^
/home/hemant/push_server/lib/db_connection.rb:59: syntax error,
unexpected keyword_when, expecting keyword_end
when :all: find_all(options)
^
/home/hemant/push_server/lib/db_connection.rb:87: syntax error,
unexpected keyword_end, expecting $end
from /home/hemant/push_server/bin/boot.rb:34:in `<top (required)>'

However if i rewrite it like this, it works:

def find(*args)
options = args.extract_options!
validate_find_options(options)
case args.first
when :first
find_first(options)
when :all
find_all(options)
else
raise "Invalid find"
end
end

Why is this? Was this intended?



9 Answers

Jesús Gabriel y Galán

12/21/2007 11:31:00 AM

0

On Dec 21, 2007 12:14 PM, hemant kumar <gethemant@gmail.com> wrote:
> I have an innocent looking code:
>
> 54: def find(*args)
> 55: options = args.extract_options!
> 56: validate_find_options(options)
> 57: case args.first
> 58: when :first: find_first(options)
> 59: when :all: find_all(options)
> 60: else raise "Invalid find"
> 61: end
> 62: end
>
>
> Now, above case statement totally blows up with Ruby 1.9:

My test is with Ruby 1.8.6 but:

>
> /home/hemant/push_server/bin/boot.rb:34:in
> `require': /home/hemant/push_server/lib/db_connection.rb:58: syntax
> error, unexpected ':', expecting keyword_then or ',' or ';' or
> '\n' (SyntaxError)
> when :first: find_first(options)

if you want to put the body of the when in the same line you need a
then keyword:

irb(main):018:0> def find(*args)
irb(main):019:1> case args.first
irb(main):020:2> when :first then puts "first"
irb(main):021:2> when :all then puts "all"
irb(main):022:2> else raise "invalid"
irb(main):023:2> end
irb(main):024:1> end
=> nil
irb(main):025:0> find :first, 1, 2, 3, 4
first
=> nil

> However if i rewrite it like this, it works:
>
> def find(*args)
> options = args.extract_options!
> validate_find_options(options)
> case args.first
> when :first
> find_first(options)
> when :all
> find_all(options)
> else
> raise "Invalid find"
> end
> end
>
> Why is this? Was this intended?

From the pickaxe:

"The then keyword (or a colon) separates the when comparisons from the
bodies and is
not needed if the body starts on a new line."

Hope this helps,

Jesus.

Lee Jarvis

12/21/2007 11:49:00 AM

0

Jesús Gabriel y Galán wrote:
> if you want to put the body of the when in the same line you need a
> then keyword:

1.8.6 works with a keyword or a colon, I think this is what Hemant
means.. 1.9 obviously doesn't support using them (I can't test it right
now)

>> def find(*args)
>> case args.first
>> when :first: puts "first"
>> when :all: puts "all"
>> else raise "invalid"
>> end
>> end
=> nil
>> find :first, 1, 2, 3, 4
first


As you can see, Hemants example works fine with colons in 1.8.6, perhaps
not in 1.9, though...

I normally use 'then' in either case..

Regards,
Lee
--
Posted via http://www.ruby-....

Lee Jarvis

12/21/2007 11:49:00 AM

0

Jesús Gabriel y Galán wrote:
> if you want to put the body of the when in the same line you need a
> then keyword:

1.8.6 works with a keyword or a colon, I think this is what Hemant
means.. 1.9 obviously doesn't support using them (I can't test it right
now)

>> def find(*args)
>> case args.first
>> when :first: puts "first"
>> when :all: puts "all"
>> else raise "invalid"
>> end
>> end
=> nil
>> find :first, 1, 2, 3, 4
first


As you can see, Hemants example works fine with colons in 1.8.6, perhaps
not in 1.9, though...

I normally use 'then' in either case..

Regards,
Lee
--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

12/21/2007 11:55:00 AM

0

On Dec 21, 2007 12:48 PM, Lee Jarvis <ljjarvis@gmail.com> wrote:
> Jes=FAs Gabriel y Gal=E1n wrote:
> > if you want to put the body of the when in the same line you need a
> > then keyword:
>
> 1.8.6 works with a keyword or a colon, I think this is what Hemant
> means.. 1.9 obviously doesn't support using them (I can't test it right
> now)
>
> >> def find(*args)
> >> case args.first
> >> when :first: puts "first"
> >> when :all: puts "all"
> >> else raise "invalid"
> >> end
> >> end
> =3D> nil
> >> find :first, 1, 2, 3, 4
> first
>
>
> As you can see, Hemants example works fine with colons in 1.8.6, perhaps
> not in 1.9, though...
> I normally use 'then' in either case..

Oh, you are right, I missed the colon after the symbol. :-(
Sorry for the noise.

Jesus.

Robert Klemme

12/21/2007 12:02:00 PM

0

2007/12/21, hemant kumar <gethemant@gmail.com>:
> I have an innocent looking code:
>
> 54: def find(*args)
> 55: options = args.extract_options!
> 56: validate_find_options(options)
> 57: case args.first
> 58: when :first: find_first(options)
> 59: when :all: find_all(options)
> 60: else raise "Invalid find"
> 61: end
> 62: end
>
>
> Now, above case statement totally blows up with Ruby 1.9:
>
> /home/hemant/push_server/lib/db_connection.rb:61: warning: else without
> rescue is useless
> /home/hemant/push_server/bin/boot.rb:34:in
> `require': /home/hemant/push_server/lib/db_connection.rb:58: syntax
> error, unexpected ':', expecting keyword_then or ',' or ';' or
> '\n' (SyntaxError)
> when :first: find_first(options)
> ^
> /home/hemant/push_server/lib/db_connection.rb:59: syntax error,
> unexpected keyword_when, expecting keyword_end
> when :all: find_all(options)
> ^
> /home/hemant/push_server/lib/db_connection.rb:87: syntax error,
> unexpected keyword_end, expecting $end
> from /home/hemant/push_server/bin/boot.rb:34:in `<top (required)>'

Does the same error surface if you precede the colon with a space?
Sorry, I don't have a 1.9 here to test this myself.

> However if i rewrite it like this, it works:
>
> def find(*args)
> options = args.extract_options!
> validate_find_options(options)
> case args.first
> when :first
> find_first(options)
> when :all
> find_all(options)
> else
> raise "Invalid find"
> end
> end
>
> Why is this? Was this intended?

Maybe the form with colon is deprecated?

Kind regards

robert


--
use.inject do |as, often| as.you_can - without end

hemant

12/21/2007 1:17:00 PM

0


On Fri, 2007-12-21 at 21:01 +0900, Robert Klemme wrote:
> 2007/12/21, hemant kumar <gethemant@gmail.com>:
> > I have an innocent looking code:
> >
> > 54: def find(*args)
> > 55: options = args.extract_options!
> > 56: validate_find_options(options)
> > 57: case args.first
> > 58: when :first: find_first(options)
> > 59: when :all: find_all(options)
> > 60: else raise "Invalid find"
> > 61: end
> > 62: end
> >
> >
> > Now, above case statement totally blows up with Ruby 1.9:
> >
> > /home/hemant/push_server/lib/db_connection.rb:61: warning: else without
> > rescue is useless
> > /home/hemant/push_server/bin/boot.rb:34:in
> > `require': /home/hemant/push_server/lib/db_connection.rb:58: syntax
> > error, unexpected ':', expecting keyword_then or ',' or ';' or
> > '\n' (SyntaxError)
> > when :first: find_first(options)
> > ^
> > /home/hemant/push_server/lib/db_connection.rb:59: syntax error,
> > unexpected keyword_when, expecting keyword_end
> > when :all: find_all(options)
> > ^
> > /home/hemant/push_server/lib/db_connection.rb:87: syntax error,
> > unexpected keyword_end, expecting $end
> > from /home/hemant/push_server/bin/boot.rb:34:in `<top (required)>'
>
> Does the same error surface if you precede the colon with a space?
> Sorry, I don't have a 1.9 here to test this myself.
>
> > However if i rewrite it like this, it works:
> >
> > def find(*args)
> > options = args.extract_options!
> > validate_find_options(options)
> > case args.first
> > when :first
> > find_first(options)
> > when :all
> > find_all(options)
> > else
> > raise "Invalid find"
> > end
> > end
> >
> > Why is this? Was this intended?
>
> Maybe the form with colon is deprecated?
>

Looks like they deprecated ":" in case statements.

Matz, shall we consider this as bug or change was unintentional?





hemant

12/21/2007 1:19:00 PM

0


On Fri, 2007-12-21 at 18:47 +0530, hemant kumar wrote:
> On Fri, 2007-12-21 at 21:01 +0900, Robert Klemme wrote:
> > 2007/12/21, hemant kumar <gethemant@gmail.com>:
> > > I have an innocent looking code:
> > >
> > > 54: def find(*args)
> > > 55: options = args.extract_options!
> > > 56: validate_find_options(options)
> > > 57: case args.first
> > > 58: when :first: find_first(options)
> > > 59: when :all: find_all(options)
> > > 60: else raise "Invalid find"
> > > 61: end
> > > 62: end
> > >
> > >
> > > Now, above case statement totally blows up with Ruby 1.9:
> > >
> > > /home/hemant/push_server/lib/db_connection.rb:61: warning: else without
> > > rescue is useless
> > > /home/hemant/push_server/bin/boot.rb:34:in
> > > `require': /home/hemant/push_server/lib/db_connection.rb:58: syntax
> > > error, unexpected ':', expecting keyword_then or ',' or ';' or
> > > '\n' (SyntaxError)
> > > when :first: find_first(options)
> > > ^
> > > /home/hemant/push_server/lib/db_connection.rb:59: syntax error,
> > > unexpected keyword_when, expecting keyword_end
> > > when :all: find_all(options)
> > > ^
> > > /home/hemant/push_server/lib/db_connection.rb:87: syntax error,
> > > unexpected keyword_end, expecting $end
> > > from /home/hemant/push_server/bin/boot.rb:34:in `<top (required)>'
> >
> > Does the same error surface if you precede the colon with a space?
> > Sorry, I don't have a 1.9 here to test this myself.
> >
> > > However if i rewrite it like this, it works:
> > >
> > > def find(*args)
> > > options = args.extract_options!
> > > validate_find_options(options)
> > > case args.first
> > > when :first
> > > find_first(options)
> > > when :all
> > > find_all(options)
> > > else
> > > raise "Invalid find"
> > > end
> > > end
> > >
> > > Why is this? Was this intended?
> >
> > Maybe the form with colon is deprecated?
> >
>
> Looks like they deprecated ":" in case statements.
>
> Matz, shall we consider this as bug or change was unintentional?

My bad.. i meant intentional, but you got the point anyways!.

Thanks




F. Senault

12/21/2007 1:53:00 PM

0

Le 21 décembre à 14:18, hemant kumar a écrit :

> My bad.. i meant intentional, but you got the point anyways!.

According to the 3rd edition of the PickAxe, it is intentional.

(p 125, Case Expressions : "Ruby 1.8 allowed you to use a colon
character in place of the then keyword. This is no longer supported.")

Fred
--
Something inside of me has opened up its eyes
Why did you put it there did you not realize
This thing inside of me it screams the loudest sound
Sometimes I think I could (Nine Inch Nails, Burn)

hemant

12/21/2007 2:07:00 PM

0


On Fri, 2007-12-21 at 22:55 +0900, F. Senault wrote:
> Le 21 décembre à 14:18, hemant kumar a écrit :
>
> > My bad.. i meant intentional, but you got the point anyways!.
>
> According to the 3rd edition of the PickAxe, it is intentional.
>
> (p 125, Case Expressions : "Ruby 1.8 allowed you to use a colon
> character in place of the then keyword. This is no longer supported.")
>

Ha ha.. damn to its decided. I don't own third edition apparently.