how can i perform a one line case statement on a number

B

Braxton Beyer

I want to be able to do something like this:

1000.case when 100 then "string1" when 110 then "string2" else "Unknown"
end

I have an app that allows an admin to enter a filter that is ruby code.
The filter code is then applied directly to a variable. Is there a way
to apply a case statement such as the one above
 
M

Mario Camou

[Note: parts of this message were removed to make it a legal post.]

Use semicolons to separate statements:

case 1000; when 100; "string1"; when 110; "string2"; else "Unknown"; end

-Mario.
 
D

David A. Black

Hi --

I want to be able to do something like this:

1000.case when 100 then "string1" when 110 then "string2" else "Unknown"
end

I have an app that allows an admin to enter a filter that is ruby code.
The filter code is then applied directly to a variable. Is there a way
to apply a case statement such as the one above

case isn't a method; it's a keyword, so you would do:

case 1000 when 100 ... end

I'm not sure I've grasped exactly what you need to do, though.


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 
P

Phrogz

I want to be able to do something like this:

1000.case when 100 then "string1" when 110 then "string2" else "Unknown"
end

irb(main):001:0> x = 1000
=> 1000

irb(main):002:0> y = case x; when 100 then "hunny"; when 110 then
"anten"; else "other" end
=> "other"

irb(main):003:0> y = x==100 ? "hunny" : x==110 ? "anten" : "other"
=> "other"
 
B

Braxton Beyer

Mario said:
Use semicolons to separate statements:

case 1000; when 100; "string1"; when 110; "string2"; else "Unknown";
end

-Mario.

well i am able to run the code on one line but my problem is being able
to apply it directly to the number. So it needs to be something like
1000.codegoeshere or 1000.eval "code"

But I am not sure how to do that.
 
B

Brian Candler

Braxton said:
I want to be able to do something like this:

1000.case when 100 then "string1" when 110 then "string2" else "Unknown"
end

I have an app that allows an admin to enter a filter that is ruby code.
The filter code is then applied directly to a variable. Is there a way
to apply a case statement such as the one above

Are you sure the 'filter' must be a method of the object itself? That
is, you are limited to methods which exist in Object and Fixnum?

(unlike, say, Liquid filters, which are defined as methods which take
the object as the first argument)
 
R

Robert Klemme

I want to be able to do something like this:

1000.case when 100 then "string1" when 110 then "string2" else "Unknown"
end

Here's a completely different but probably more efficient variant:

irb(main):004:0> h=Hash.new("Unknown").merge(100=>"string1",110=>"string2")
=> {100=>"string1", 110=>"string2"}
irb(main):005:0> h[100]
=> "string1"
irb(main):006:0> h[110]
=> "string2"
irb(main):007:0> h[120]
=> "Unknown"
irb(main):008:0>
I have an app that allows an admin to enter a filter that is ruby code.
The filter code is then applied directly to a variable. Is there a way
to apply a case statement such as the one above

Given eval anything is possible with code entered via the terminal. You
could do

irb(main):010:0> val = 100
=> 100
irb(main):011:0> input = "case when 110 then 'foo' else 'bar' end"
=> "case when 110 then 'foo' else 'bar' end"
irb(main):012:0> prepared = input.gsub(/case/, '\\& val')
=> "case val when 110 then 'foo' else 'bar' end"
irb(main):013:0> eval(prepared)
=> "bar"

Although that approach would need to be more complex for other
scenarios. And you need to be aware of the security implications of
using eval.

Kind regards

robert
 
B

Braxton Beyer

Are you sure the 'filter' must be a method of the object itself? That
is, you are limited to methods which exist in Object and Fixnum?

(unlike, say, Liquid filters, which are defined as methods which take
the object as the first argument)

Currently, it does have to be a method of the object itself. But it
appears that may be to limiting. It osunds like Liquid filters amy be
what I need. Where can I get more info on those?
 
B

Braxton Beyer

Thanks for the help. I decided to change the code to allow me to pass
the variable into a function instead of being forced to use an existing
method of the object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top