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

···

--
Posted via http://www.ruby-forum.com/.

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.

On Sat, Oct 31, 2009 at 22:10, Braxton Beyer <rubyforum@kidbrax.oib.com>wrote:

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

Hi --

···

On Sun, 1 Nov 2009, Braxton Beyer 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)

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"

···

On Oct 31, 3:10 pm, Braxton Beyer <rubyfo...@kidbrax.oib.com> wrote:

I want to be able to do something like this:

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

Braxton Beyer 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)

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 10/31/2009 10:10 PM, Braxton Beyer wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Mario Camou 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.

···

--
Posted via http://www.ruby-forum.com/.

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?

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.