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
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
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”
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)
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.
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?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.