[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby case statement

tobyclemson@gmail.com

7/4/2007 10:00:00 AM

Hi,

I've read through some of the other case statement posts but can't
find any that answer me this question.

I know that the case doesn't allow the follow through aspect of case
statements from other languages but is the following valid:

case type
when 1
# do stuff for type 1
when 1,2
# do stuff for both type 1 and 2
end

If it isn't valid, what is the easiest way to obtain this
functionality

Thanks,
Toby


12 Answers

Robert Klemme

7/4/2007 10:33:00 AM

0

2007/7/4, tobyclemson@gmail.com <tobyclemson@gmail.com>:
> Hi,
>
> I've read through some of the other case statement posts but can't
> find any that answer me this question.
>
> I know that the case doesn't allow the follow through aspect of case
> statements from other languages but is the following valid:
>
> case type
> when 1
> # do stuff for type 1
> when 1,2
> # do stuff for both type 1 and 2
> end
>
> If it isn't valid, what is the easiest way to obtain this
> functionality

Why don't you just try it out?

Btw, what is the "type" that you are checking? I ask because if you
want to do something based on the class of an instance there are
different means.

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fu...

David and Sharon Phillips

7/4/2007 10:37:00 AM

0

> case type
> when 1
> # do stuff for type 1
> when 1,2
> # do stuff for both type 1 and 2
> end
>
> If it isn't valid, what is the easiest way to obtain this
> functionality
>

i=1
case i
when 1: puts "1"
when 1,2: puts "1 or 2"
when 3: puts "3"
else puts "other"
end

> 1

so it looks like the second one gets skipped.

The best i can come up with (but I'm pretty sure someone else will
come up with a much better alternative) is

i=1
case i
when 1,2
puts "1 or 2"
puts "1" if i==1
when 3
puts "3"
else
puts "other"
end

> 1 or 2
> 1

Cheers,
Dave


tobyclemson@gmail.com

7/4/2007 11:04:00 AM

0

Sorry type was misleading - the variable could be anything, I'm not
doing anything to do with the class of an instance. I had tried it out
but I'm quite new to ruby so I doubted myself and thought I'd ask
those in the know!

On Jul 4, 11:32 am, "Robert Klemme" <shortcut...@googlemail.com>
wrote:
> 2007/7/4, tobyclem...@gmail.com <tobyclem...@gmail.com>:
>
>
>
> > Hi,
>
> > I've read through some of the other case statement posts but can't
> > find any that answer me this question.
>
> > I know that the case doesn't allow the follow through aspect of case
> > statements from other languages but is the following valid:
>
> > case type
> > when 1
> > # do stuff for type 1
> > when 1,2
> > # do stuff for both type 1 and 2
> > end
>
> > If it isn't valid, what is the easiest way to obtain this
> > functionality
>
> Why don't you just try it out?
>
> Btw, what is the "type" that you are checking? I ask because if you
> want to do something based on the class of an instance there are
> different means.
>
> Kind regards
>
> robert
>
> --
> Have a look:http://www.flickr.com/photos/fu...


tobyclemson@gmail.com

7/4/2007 11:05:00 AM

0

Thanks, that's neater than the ways of doing it that I have found
since first posting.

On Jul 4, 11:36 am, Sharon Phillips <phillip...@yahoo.co.uk> wrote:
> > case type
> > when 1
> > # do stuff for type 1
> > when 1,2
> > # do stuff for both type 1 and 2
> > end
>
> > If it isn't valid, what is the easiest way to obtain this
> > functionality
>
> i=1
> case i
> when 1: puts "1"
> when 1,2: puts "1 or 2"
> when 3: puts "3"
> else puts "other"
> end
>
> > 1
>
> so it looks like the second one gets skipped.
>
> The best i can come up with (but I'm pretty sure someone else will
> come up with a much better alternative) is
>
> i=1
> case i
> when 1,2
> puts "1 or 2"
> puts "1" if i==1
> when 3
> puts "3"
> else
> puts "other"
> end
>
> > 1 or 2
> > 1
>
> Cheers,
> Dave


Robert Klemme

7/4/2007 5:19:00 PM

0

On 04.07.2007 13:03, tobyclemson@gmail.com wrote:
> Sorry type was misleading - the variable could be anything, I'm not
> doing anything to do with the class of an instance. I had tried it out
> but I'm quite new to ruby so I doubted myself and thought I'd ask
> those in the know!

I probably was too quick on the trigger, I am sorry. I completely
overlooked your "fall through" issue. Personally I have used that in
other languages but have yet to miss it in Ruby.

It's sometimes difficult to just translate an idiom from another
programming language. And more often than not the resulting code is
either ugly, inefficient or could be done more elegantly. Of course
there are solutions using "case" and other Ruby constructs that would
achieve what you want but it's probably more helpful for you if you can
present a real problem that you are trying to solve and we can come up
with a solution.

This is a simple solution, i.e. separate the task into several steps and
do control flow for each step:

type = 1
puts 1 if type == 1
case type
when 1,2: puts "1 or 2"
when 3: puts 3
else puts "other"
end

Kind regards

robert

tobyclemson@gmail.com

7/6/2007 8:47:00 AM

0

Basically I had a situation where dependent on the formatting of a
block of text, different actions had to be carried out but some
actions were common to each of the formattings whereas others weren't
so I really wanted to be able to apply the bits that were specific to
say the type 1 format first and then follow down into the actions
common to both type 1 and type 2 such as is the case in other
languages when you don't 'break' out of each 'case' clause. I
understand now that all 'case' clauses in ruby automatically 'break'
when they finish but I was wondering whether there was a way around
that such as in my code example. It appears though that in my code
block, the first 'when' clause fulfilling the condition is executed
and then the 'case' is exited. I wanted to know whether there was a
way to stop the 'case' exiting so that it continues and carries out
the actions in the next 'when' clause fulfilling the condition. And if
there isn't a way to do this, what is the best way to achieve the
functionality I am trying to obtain.

Thanks
Toby

On Jul 6, 5:14 am, "list. rb" <list...@gmail.com> wrote:
> I agree. --Not so sure a case statement makes sense here. Tell us what you
> are trying to accomplish. Is it to get clarity on ruby's case statements? or
> to really follow the logic you've detailed?
>
> puts i if [1,3].include? i
> puts "1 or 2" if [1,2].include? i
>
> On 7/5/07, Nasir Khan <rubylear...@gmail.com> wrote:
>
>
>
> > when 1: puts "1"
> > when 1,2: puts "1 or 2"
>
> > Logically does'nt make much sense.
> > Here "when 1" is redundant, your puts "1 or 2" will never be printed even
> > when value is either 1 or 2 as you want.
>
> > The question is - What are you actually trying to do?
>
> > On 7/4/07, tobyclem...@gmail.com <tobyclem...@gmail.com> wrote:
>
> > > Sorry type was misleading - the variable could be anything, I'm not
> > > doing anything to do with the class of an instance. I had tried it out
> > > but I'm quite new to ruby so I doubted myself and thought I'd ask
> > > those in the know!
>
> > > On Jul 4, 11:32 am, "Robert Klemme" <shortcut...@googlemail.com>
> > > wrote:
> > > > 2007/7/4, tobyclem...@gmail.com <tobyclem...@gmail.com>:
>
> > > > > Hi,
>
> > > > > I've read through some of the other case statement posts but can't
> > > > > find any that answer me this question.
>
> > > > > I know that the case doesn't allow the follow through aspect of case
> > > > > statements from other languages but is the following valid:
>
> > > > > case type
> > > > > when 1
> > > > > # do stuff for type 1
> > > > > when 1,2
> > > > > # do stuff for both type 1 and 2
> > > > > end
>
> > > > > If it isn't valid, what is the easiest way to obtain this
> > > > > functionality
>
> > > > Why don't you just try it out?
>
> > > > Btw, what is the "type" that you are checking? I ask because if you
> > > > want to do something based on the class of an instance there are
> > > > different means.
>
> > > > Kind regards
>
> > > > robert
>
> > > > --
> > > > Have a look:http://www.flickr.com/photos/fu...
>
>


Robert Klemme

7/6/2007 9:20:00 AM

0

2007/7/6, tobyclemson@gmail.com <tobyclemson@gmail.com>:
> Basically I had a situation where dependent on the formatting of a
> block of text, different actions had to be carried out but some
> actions were common to each of the formattings whereas others weren't
> so I really wanted to be able to apply the bits that were specific to
> say the type 1 format first and then follow down into the actions
> common to both type 1 and type 2 such as is the case in other
> languages when you don't 'break' out of each 'case' clause. I
> understand now that all 'case' clauses in ruby automatically 'break'
> when they finish but I was wondering whether there was a way around
> that such as in my code example. It appears though that in my code
> block, the first 'when' clause fulfilling the condition is executed
> and then the 'case' is exited. I wanted to know whether there was a
> way to stop the 'case' exiting so that it continues and carries out
> the actions in the next 'when' clause fulfilling the condition. And if
> there isn't a way to do this, what is the best way to achieve the
> functionality I am trying to obtain.

I do not know how complex your operations are. From what you write I
guess there could be multiple actions and they could be complex. Also,
there might be numerous formats - if not today then maybe in a later
stage. Going from there the solution I'd probably favor is a class
hierarchy of formatters. That way you can define some basic
formatting functionality in methods in the base class and use those
methods from all sub class methods.

You then only need to map your format code in some way to the name of
a specific formatter class and send the request off to that to get
your specific formatting. For the mapping you can use various
mechanisms, the easiest probably being the class name of the
formatter. But you can as well use some specific mapping using a
Hash. There are other methods as well.

Kind regards

robert

John Joyce

7/6/2007 9:07:00 PM

0


On Jul 6, 2007, at 4:19 AM, Robert Klemme wrote:

> 2007/7/6, tobyclemson@gmail.com <tobyclemson@gmail.com>:
>> Basically I had a situation where dependent on the formatting of a
>> block of text, different actions had to be carried out but some
>> actions were common to each of the formattings whereas others weren't
>> so I really wanted to be able to apply the bits that were specific to
>> say the type 1 format first and then follow down into the actions
>> common to both type 1 and type 2 such as is the case in other
>> languages when you don't 'break' out of each 'case' clause. I
>> understand now that all 'case' clauses in ruby automatically 'break'
>> when they finish but I was wondering whether there was a way around
>> that such as in my code example. It appears though that in my code
>> block, the first 'when' clause fulfilling the condition is executed
>> and then the 'case' is exited. I wanted to know whether there was a
>> way to stop the 'case' exiting so that it continues and carries out
>> the actions in the next 'when' clause fulfilling the condition.
>> And if
>> there isn't a way to do this, what is the best way to achieve the
>> functionality I am trying to obtain.
>
> I do not know how complex your operations are. From what you write I
> guess there could be multiple actions and they could be complex. Also,
> there might be numerous formats - if not today then maybe in a later
> stage. Going from there the solution I'd probably favor is a class
> hierarchy of formatters. That way you can define some basic
> formatting functionality in methods in the base class and use those
> methods from all sub class methods.
>
> You then only need to map your format code in some way to the name of
> a specific formatter class and send the request off to that to get
> your specific formatting. For the mapping you can use various
> mechanisms, the easiest probably being the class name of the
> formatter. But you can as well use some specific mapping using a
> Hash. There are other methods as well.
>
> Kind regards
>
> robert
>
you mean you want it to fall through like in C?

Robert Klemme

7/7/2007 5:06:00 PM

0

On 06.07.2007 23:06, John Joyce wrote:
>
> On Jul 6, 2007, at 4:19 AM, Robert Klemme wrote:
>
>> 2007/7/6, tobyclemson@gmail.com <tobyclemson@gmail.com>:
>>> Basically I had a situation where dependent on the formatting of a
>>> block of text, different actions had to be carried out but some
>>> actions were common to each of the formattings whereas others weren't
>>> so I really wanted to be able to apply the bits that were specific to
>>> say the type 1 format first and then follow down into the actions
>>> common to both type 1 and type 2 such as is the case in other
>>> languages when you don't 'break' out of each 'case' clause. I
>>> understand now that all 'case' clauses in ruby automatically 'break'
>>> when they finish but I was wondering whether there was a way around
>>> that such as in my code example. It appears though that in my code
>>> block, the first 'when' clause fulfilling the condition is executed
>>> and then the 'case' is exited. I wanted to know whether there was a
>>> way to stop the 'case' exiting so that it continues and carries out
>>> the actions in the next 'when' clause fulfilling the condition. And if
>>> there isn't a way to do this, what is the best way to achieve the
>>> functionality I am trying to obtain.
>>
>> I do not know how complex your operations are. From what you write I
>> guess there could be multiple actions and they could be complex. Also,
>> there might be numerous formats - if not today then maybe in a later
>> stage. Going from there the solution I'd probably favor is a class
>> hierarchy of formatters. That way you can define some basic
>> formatting functionality in methods in the base class and use those
>> methods from all sub class methods.
>>
>> You then only need to map your format code in some way to the name of
>> a specific formatter class and send the request off to that to get
>> your specific formatting. For the mapping you can use various
>> mechanisms, the easiest probably being the class name of the
>> formatter. But you can as well use some specific mapping using a
>> Hash. There are other methods as well.
>>
>> Kind regards
>>
>> robert
>>
> you mean you want it to fall through like in C?

No. This is a completely different approach which has nothing to do
with "case".

robert

Robert Klemme

7/9/2007 9:23:00 AM

0

Apparently my newsgroup reply has not made it to the mailing list
(yet). Apologies if you receive two answers.

2007/7/6, John Joyce <dangerwillrobinsondanger@gmail.com>:
>
> On Jul 6, 2007, at 4:19 AM, Robert Klemme wrote:
>
> > 2007/7/6, tobyclemson@gmail.com <tobyclemson@gmail.com>:
> >> Basically I had a situation where dependent on the formatting of a
> >> block of text, different actions had to be carried out but some
> >> actions were common to each of the formattings whereas others weren't
> >> so I really wanted to be able to apply the bits that were specific to
> >> say the type 1 format first and then follow down into the actions
> >> common to both type 1 and type 2 such as is the case in other
> >> languages when you don't 'break' out of each 'case' clause. I
> >> understand now that all 'case' clauses in ruby automatically 'break'
> >> when they finish but I was wondering whether there was a way around
> >> that such as in my code example. It appears though that in my code
> >> block, the first 'when' clause fulfilling the condition is executed
> >> and then the 'case' is exited. I wanted to know whether there was a
> >> way to stop the 'case' exiting so that it continues and carries out
> >> the actions in the next 'when' clause fulfilling the condition.
> >> And if
> >> there isn't a way to do this, what is the best way to achieve the
> >> functionality I am trying to obtain.
> >
> > I do not know how complex your operations are. From what you write I
> > guess there could be multiple actions and they could be complex. Also,
> > there might be numerous formats - if not today then maybe in a later
> > stage. Going from there the solution I'd probably favor is a class
> > hierarchy of formatters. That way you can define some basic
> > formatting functionality in methods in the base class and use those
> > methods from all sub class methods.
> >
> > You then only need to map your format code in some way to the name of
> > a specific formatter class and send the request off to that to get
> > your specific formatting. For the mapping you can use various
> > mechanisms, the easiest probably being the class name of the
> > formatter. But you can as well use some specific mapping using a
> > Hash. There are other methods as well.
> >
> > Kind regards
> >
> > robert
> >
> you mean you want it to fall through like in C?

No. My suggestion has nothing to do with "case" statements. It's an
OO way of doing this.

Kind regards

robert