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

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

Use semicolons to separate statements:

case 1000; when 100; “string1”; when 110; “string2”; else “Unknown”;
end

-Mario.


I want to change the world but they won’t give me the source code.

Hi –

On Sun, 1 Nov 2009, Braxton B. wrote:

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)

On Oct 31, 3:10 pm, Braxton B. [email protected] wrote:

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”

Mario C. wrote:

Use semicolons to separate statements:

case 1000; when 100; “string1”; when 110; “string2”; else “Unknown”;
end

-Mario.


I want to change the world but they won’t give me the source code.

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.

Although the case syntax in Ruby is awesome, they is usually an equally
good alternative, such as

{100 => “string1”, 110 => “string2”}[1000] || “Unknown”

Braxton B. wrote:

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)

On 10/31/2009 10:10 PM, Braxton B. wrote:

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

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.

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?