[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[Q] case based on object class

George Moschovitis

10/11/2004 9:35:00 AM

Hello everyone,

I wonder if there is a more elegant/efficient way to rewrite
the case structure in the following code snippet:

def myfunc(obj)
case obj.class.name
when Fixnum.name
puts :Fixnum
when Float.name
puts :Float
...
default
puts :Object
end
end

thanks in advance,

George Moschovitis


--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

Navel does not accept liability for any errors, viruses or omissions in
the contents of this message. The full corporate policy is available on
our site.

have fun: www.joy.gr
13 Answers

Michael Neumann

10/11/2004 9:47:00 AM

0

George Moschovitis wrote:
> Hello everyone,
>
> I wonder if there is a more elegant/efficient way to rewrite
> the case structure in the following code snippet:
>
> def myfunc(obj)
> case obj.class.name
> when Fixnum.name
> puts :Fixnum
> when Float.name
> puts :Float
> ...
> default
> puts :Object
> end
> end

case obj
when Fixnum
puts obj.class.to_s.to_sym
when Float
...
else
puts :Object
end


Regards,

Michael


Hal E. Fulton

10/11/2004 9:47:00 AM

0

George Moschovitis wrote:
> Hello everyone,
>
> I wonder if there is a more elegant/efficient way to rewrite
> the case structure in the following code snippet:

Certainly... see below.

> def myfunc(obj)
> case obj
> when Fixnum
> puts :Fixnum
> when Float
> puts :Float
> ...
> default
> puts :Object
> end
> end

This works because case invokes ===, which takes a minute
or three to understand. :)


Hal

PS - In my Mozilla, your font is microscopic. :0


H



George Moschovitis

10/11/2004 9:54:00 AM

0

> This works because case invokes ===, which takes a minute
> or three to understand. :)

Thank you Hal and Michael!

Ruby can be VERY surprising after all. After the initial shock it
feels natural though.

-g.

--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

Navel does not accept liability for any errors, viruses or omissions in
the contents of this message. The full corporate policy is available on
our site.

have fun: www.joy.gr

George Moschovitis

10/11/2004 10:00:00 AM

0

Hmm my example was wrong, what I wanted to write is:

def myfunc(klass)
case klass.name
when Fixnum.name
puts :Fixnum
when Float.name
puts :Float
...
default
puts :Object
end
end

myfunc(obj.class)

Now this === doesnt work :( I am sure the solution is obvious,
but I would appreciate some help.

regards,
George Moschovitis


--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

Navel does not accept liability for any errors, viruses or omissions in
the contents of this message. The full corporate policy is available on
our site.

have fun: www.joy.gr

Hal E. Fulton

10/11/2004 10:19:00 AM

0

George Moschovitis wrote:
> Hmm my example was wrong, what I wanted to write is:
>
> def myfunc(klass)
> case klass.name
> when Fixnum.name
> puts :Fixnum
> when Float.name
> puts :Float
> ...
> default
> puts :Object
> end
> end
>
> myfunc(obj.class)
>
> Now this === doesnt work :( I am sure the solution is obvious,
> but I would appreciate some help.

I am not sure what you want to do... do you want to separate these
things by class and print out the corresponding symbol?

Anyhow, I tried your code and it worked fine for me. I substituted
4 for obj and commented the ... of course.

What's the problem about?


Hal



Hal E. Fulton

10/11/2004 10:26:00 AM

0

Hal Fulton wrote:
> Anyhow, I tried your code and it worked fine for me. I substituted
> 4 for obj and commented the ... of course.

Also, what's this 'default' thing? I'm surprised it didn't give a
syntax error... 'else' is the thing.

I guess it's interpreting that as a method call but you're never
reaching it.


Hal



George Moschovitis

10/11/2004 10:26:00 AM

0

> I am not sure what you want to do... do you want to separate these
> things by class and print out the corresponding symbol?

i want to execute different code depending on the klass (Class) object
given to the method.

> Anyhow, I tried your code and it worked fine for me. I substituted
> 4 for obj and commented the ... of course.

the code works, I am wondering if there is a more elegant/efficient
way to write it.

-g.

--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

Navel does not accept liability for any errors, viruses or omissions in
the contents of this message. The full corporate policy is available on
our site.

have fun: www.joy.gr

George Moschovitis

10/11/2004 10:34:00 AM

0

> Also, what's this 'default' thing? I'm surprised it didn't give a

hmm that was an error, I thought I was writing C or something.
it was pseudocode anyway, just to illustrate my question...

-g.

--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

Navel does not accept liability for any errors, viruses or omissions in
the contents of this message. The full corporate policy is available on
our site.

have fun: www.joy.gr

Hal E. Fulton

10/11/2004 10:37:00 AM

0

George Moschovitis wrote:
>
> the code works, I am wondering if there is a more elegant/efficient
> way to write it.

Well, I'd wonder why you're testing the class instead of the
object.

def myfunc(obj)
case obj
when Fixnum
# ...
when Float
# ...
else
# ...
end
end


myfunc(some_obj)


This seems better to me...


Hal



T. Onoma

10/11/2004 10:52:00 AM

0

On Monday 11 October 2004 06:04 am, George Moschovitis wrote:| def myfunc(klass)|          case klass.name|                  when Fixnum.name|                          puts :Fixnum|                  when Float.name|                          puts :Float|                  ...|                  default|                          puts :Object|          end| end
This doesn't work?
def myfunc(klass)     case klass             when Fixnum                  puts :Fixnum              when Float                 puts :Float              ...             default                 puts :Object          end end

BTW the method being used by case in the above is Class#===, to be more exact it is calling Fixnum#=== and Float#===
T.