hi guys,
was wondering the syntax of how to do this:
case [1,2,3,4]
when 1: 'this is what i want'
when 11: 'not result'
when 15: 'no good'
end
((i.e, whatever value is included in the array))
...how do i do this?
tia
···
--
Posted via http://www.ruby-forum.com/.
Shai Rosenfeld wrote:
hi guys,
was wondering the syntax of how to do this:
case [1,2,3,4]
when 1: 'this is what i want'
when 11: 'not result'
when 15: 'no good'
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more fleshed-out example might help?
···
--
Alex
hi guys,
was wondering the syntax of how to do this:
case [1,2,3,4]
when 1: 'this is what i want'
when 11: 'not result'
when 15: 'no good'
end
((i.e, whatever value is included in the array))
...how do i do this?
Strange problem you have here... What if array includes all: 1,11,15?
What the result should be?
choices = {1 => 'this is what i want', 11 => 'not result', 15 => 'no good'}
data = [1, 2, 3, 4]
data.inject(){|result, k| result << choices[k]}.compact
=> ["this is what i want"]
Regards,
Rimantas
···
--
http://rimantas.com/
You can do it with the other form of "case":
a = [1,2,3,4]
result = case
when a.include?(1): 'this is what i want'
when a.include?(11): 'not result'
when a.include?(15): 'no good'
end
Kind regards
robert
···
2007/8/8, Shai Rosenfeld <shaiguitar@gmail.com>:
hi guys,
was wondering the syntax of how to do this:
case [1,2,3,4]
when 1: 'this is what i want'
when 11: 'not result'
when 15: 'no good'
end
((i.e, whatever value is included in the array))
...how do i do this?
Alex Young wrote:
Shai Rosenfeld wrote:
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
(the above doesn't work. it's gives a 'not enough arguments' error. how
do i do it correctly?)
···
--
Posted via http://www.ruby-forum.com/\.
Robert Klemme wrote:
hi guys,
was wondering the syntax of how to do this:
case [1,2,3,4]
when 1: 'this is what i want'
when 11: 'not result'
when 15: 'no good'
end
((i.e, whatever value is included in the array))
...how do i do this?
You can do it with the other form of "case":
a = [1,2,3,4]
result = case
when a.include?(1): 'this is what i want'
when a.include?(11): 'not result'
when a.include?(15): 'no good'
end
Or the other way around:
a = [1,2,3,4]
b = 2
result = case b
when *a: "yes!"
else "no!"
end
···
2007/8/8, Shai Rosenfeld <shaiguitar@gmail.com>:
--
Alex
a = [1,2,3,4]
result = case
when a.include?(1): 'this is what i want'
when a.include?(11): 'not result'
when a.include?(15): 'no good'
end
ahhhhhhhhhhhhhhhh
perfect.
thanks.
···
--
Posted via http://www.ruby-forum.com/\.
include? takes an argument x, and is called on a collection n, so that
if n.include?(x) it returns true.
http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_array.html#Array.include_qm
···
On Aug 8, 10:05 am, Shai Rosenfeld <shaigui...@gmail.com> wrote:
Alex Young wrote:
> Shai Rosenfeld wrote:
>> end
>> ((i.e, whatever value is included in the array))
>> ...how do i do this?
> Does the Array#include? method do what you need? Perhaps a more
> fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
(the above doesn't work. it's gives a 'not enough arguments' error. how
do i do it correctly?)
--
Posted viahttp://www.ruby-forum.com/.
You could possibly do something like the following, but it's pretty dangerous:
class Object
alias_method :old_case_equal, :===
def ===(other)
case other
when Array:
other.include? self
else
old_case_equal(other)
end
end
end
puts case [3, 45, 6, 'abc']
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
I.e. case calls === on each when condition, so if you define proper
=== that will not break other things, you're done.
when you run
case a
when b: ...
when c: ...
end
ruby will evaulate
b === a, i.e. b.===(a)
c === a, i.e. c.===(a)
J.
···
On 8/8/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
Alex Young wrote:
> Shai Rosenfeld wrote:
>> end
>>
>> ((i.e, whatever value is included in the array))
>> ...how do i do this?
>
> Does the Array#include? method do what you need? Perhaps a more
> fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the
drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
(the above doesn't work. it's gives a 'not enough arguments' error. how
do i do it correctly?)
Shai Rosenfeld wrote:
Alex Young wrote:
Shai Rosenfeld wrote:
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
I'm not sure why you're using case here. I'd do it like this:
[3, 45, 6, 'abc'].include?(var) ? "good" : "no good"
···
--
Alex
Shai Rosenfeld wrote:
Alex Young wrote:
Shai Rosenfeld wrote:
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
(the above doesn't work. it's gives a 'not enough arguments' error. how do i do it correctly?)
Here's is a little trick that works
class Casey
def initialize(o)
@o = o
end
def method_missing(name)
CaseyMethod.new(@o, name)
end
end
class CaseyMethod
def initialize(o, m)
@o = o
@m = m
end
def ==(other)
@o.send(@m, other)
end
end
class Object
def casey
Casey.new(self)
end
end
case [2,3,4].casey.include?
when 1
puts "a"
when 2
puts "b"
else
puts "c"
end
···
--
Brad Phelan
http://xtargets.com
Kaldrenon wrote:
Array#include is EXACTLY what i need, but syntaxtetically (if u get the
--
Posted viahttp://www.ruby-forum.com/.
include? takes an argument x, and is called on a collection n, so that
if n.include?(x) it returns true.
http://www.whytheluckystiff.net/ruby/pickaxe/html/ref_c_array.html#Array.include_qm
please read the post before posting yourself.
what i need is the syntax of a case statement doing the following:
case [a, b, c, d]
when x: 'nothing happens'
when y: 'nothing happens'
when z: 'nothing happens'
when a: 'render this option'
end
can anyone help me with a live code example of how to do this?
many thanks.
···
On Aug 8, 10:05 am, Shai Rosenfeld <shaigui...@gmail.com> wrote:
--
Posted via http://www.ruby-forum.com/\.
Hi --
Shai Rosenfeld wrote:
Alex Young wrote:
Shai Rosenfeld wrote:
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
I'm not sure why you're using case here. I'd do it like this:
[3, 45, 6, 'abc'].include?(var) ? "good" : "no good"
I think the problem is that if it includes both 1 and 3, it's no good.
You (Shai) could do something like:
module YesNo
def yes_no(a,b)
[*a].all? {|e| include?(a) } and not [*b].any? {|e| include?(e) }
end
end
a = [1,2,3,4,5]
a.extend(YesNo)
p a.yes_no(3,6) # true
p a.yes_no(3,5) # false
(Those two lines are my only tests at the moment so try it out some
more
David
···
On Wed, 8 Aug 2007, Alex Young wrote:
--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
Brad Phelan wrote:
Shai Rosenfeld wrote:
Alex Young wrote:
Shai Rosenfeld wrote:
end
((i.e, whatever value is included in the array))
...how do i do this?
Does the Array#include? method do what you need? Perhaps a more
fleshed-out example might help?
Array#include is EXACTLY what i need, but syntaxtetically (if u get the drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
(the above doesn't work. it's gives a 'not enough arguments' error. how do i do it correctly?)
Here's is a little trick that works
class Casey
def initialize(o)
@o = o
end
def method_missing(name)
CaseyMethod.new(@o, name)
end
end
class CaseyMethod
def initialize(o, m)
@o = o
@m = m
end
def ==(other)
@o.send(@m, other)
end
end
class Object
def casey
Casey.new(self)
end
end
case [2,3,4].casey.include?
when 1
puts "a"
when 2
puts "b"
else
puts "c"
end
--
Brad Phelan
http://xtargets.com
The problem with the above is that I don't think it does what you want. It breaks out after it finds the first match and doesn't try to match
any further. If more than one *when* expression matches only the
first block is executed.
···
--
Brad Phelan
http://xtargets.com
Alex Young wrote:
Shai Rosenfeld wrote:
drift) i'm not sure how to do it:
case [3, 45, 6, 'abc'].inlcude?
when 1: 'no good'
when 3: 'good!'
when 'lolo': 'no good'
end
I'm not sure why you're using case here. I'd do it like this:
[3, 45, 6, 'abc'].include?(var) ? "good" : "no good"
...went for this in the end. i suppose the case statement w/ array
include opt didn't really fit the needs, all in all. thanks all for your
answers though.
David, what is this all? method? is it a Array#method ? i didn't find it
in the docs..?
[*a].all? {|e| include?(a) } and not [*b].any? {|e| include?(e) }
···
--
Posted via http://www.ruby-forum.com/\.
Shai Rosenfeld wrote:
what is this all? method? is it a Array#method ? i didn't find it
in the docs..?
It's Enumerable#all?. It returns true if the block evaluates to true for all
elements (or in case there is no block, if all the elements are true (i.e.
non-nil/false)
HTH,
Sebastian
···
--
NP: Explosions in the Sky - First Breath After Coma
Jabber: sepp2k@jabber.org
ICQ: 205544826