Ruby: conditional matrix? case with multiple conditions?
Boolean case (with no expression in the case
, it returns the first branch with a truthy when_expr
):
result = case
when A && B then ...
when A && !B then ...
when !A && B then ...
when !A && !B then ...
end
Matching case (with an expression in the case
, it returns the first branch that satisfies the predicate when_expr === case_expr
):
result = case [A, B]
when [true, true] then ...
when [true, false] then ...
when [false, true] then ...
when [false, false] then ...
end
Ruby: Two conditions in Case statement
The conditions in a case
statement are not Boolean expressions; they are values that are compared against the target object using the ===
operator. Of course, any class can override that operator, but the predefined one only returns true for a small set of conditions (like is_a?
, equals, is contained in, =~
...). Having a Boolean expression as the when
condition doesn't trigger a match when that expression is true; it triggers a match when the target object ===
the value of the expression, and for most objects, obj === true
is not itself true.
So you might want to swap out your case
for an if
/else
chain in this instance:
if a.is_a? String
puts 'string'
elsif a.is_a?(Array) && a.all? { |obj| obj.is_a? String }
puts 'an array with only string classes'
else
raise ArgumentError, "unsupported object #{a}:#{a.class}"
end
Or do a nested check inside the case condition. But then you would have two else
states, one for the if
(meaning it matched the array case but wasn't all strings) and one for the case
(meaning it was something else entirely). So rather than duplicate the error-handling code like that, I used a boolean variable to indicate when an error needed to be raise
d:
ok = false
case a
when String
puts 'string'
ok = true
when Array
if a.all? { |obj| obj.is_a? String }
puts 'an array with only string classes'
ok = true
end
end
if !ok
raise ArgumentError, "unsupported object #{a}:#{a.class}"
end
How does a ruby case statement with multiple conditions work
If you want the exact substitute of if
-elsif
functionality, you should use empty case
condition:
case
when @mails_hash.include?(mail) && @mails_hash[mail] > 0
p "true"
true
when @mails_hash.include?(mail) && @mails_hash[mail] == 0
@mails_hash[mail] =+ 1
p "false double"
false
else
p "false else"
false
end
When you put an argument in call to case
, it’s used to compare against when
clauses using threeequals aka case-equal:
case mail
when MailClass then ...
end
The code above actually calls MailClass.===(mail)
.
Multiple conditions in if statement Ruby
Following line looks wrong,
array[4][4].include?(mark) &&
Remove &&
Ruby if statement multiple conditions not equal
I think what you are trying to achieve, can be done by-
H.each do |key, v|
if (key != "a") && (key != "b")
puts key
end
end
Note - ||
operator will return true if any of the two conditions is true.
Hope that helps.
How do I write a complex multi-line if condition in Ruby?
The short answer is the operator needs to be at the end of the line in order to tell Ruby to continue reading the next line as part of the statement, so this would work:
if ( (aa != nil && self.prop1 == aa.decrypt) ||
(bb != nil && self.prop2 == bb.decrypt) ) &&
(self.id.nil? || self.id != id)
return true
end
That being said, you can probably reduce the logic by throwing exceptions based on input values, and removing some redundant checks (I'm making some jumps here about what your variables will look like, but you get the idea.)
raise 'aa must support decrypt' unless aa.respond_to? :decrypt
raise 'bb must support decrypt' unless bb.respond_to? :decrypt
if prop1 == aa.decrypt || prop2 == bb.decrypt
if self.id != id
return true
end
end
Related Topics
Case-Insensitive Array#Include
Ruby: Sum Corresponding Members of Two or More Arrays
How to Track Down a Memory Leak in My Ruby Code
Creating Routes with an Optional Path Prefix
Best Ruby Idiom for "Nil or Zero"
When to Use a Lambda in Ruby on Rails
Where in the Ruby Language Is %Q, %W, etc., Defined
How to Measure the Size of a Ruby Object
Executing User-Supplied Ruby Code on a Web Server