Can the operator "if (a == b || c == b)" be made shorter in Ruby

I have a piece of code in Ruby that looks like this:

def check if a == b || c == b # execute some code # b = the same variable end end 

can it be written as

 def check if a || c == b # this doesn't do the trick end if (a || c) == b # this also doesn't do the magic as I thought it would end end 

Or in the form in which I do not need to enter b twice. This is out of laziness, and I would like to know.

+8
ruby
source share
6 answers
 if [a, c].include? b # code end 

This, however, is significantly slower than the code you want to avoid β€” at least as long as a , b and c are the underlying data. My measurements showed a coefficient of 3. This is probably due to the additional creation of an Array object. Therefore, you may have to weigh DRY versus performance here. Usually this does not matter, because both options do not take much time.

+20
source share

Although this is not the exact equivalent of a == b || a == c a == b || a == c , the case offers syntax for this:

 case a when b, c then puts "It b or c." else puts "It something else." end 

Remember to open the nearest Ruby tutorial and read about how the case statement works. Spoiler: it works by calling the #=== method for the compared objects:

 a = 42 b, c = Object.new, Object.new def b.=== other; puts "b#=== called" end def c.=== other; puts "c#=== called" end 

Now run

 case a when b, c then true else false end 

This gives you great flexibility. This requires work in the back office, but after you do this, it looks like magic in the front office.

+6
source share

You really need to know why this is not working:

 (a || c) == b 

This is similar to translating the sentence "a or c equals b", which makes sense in English.

In almost all programming languages (a || c) is an expression whose evaluated result will be compared with b . Translation into English means "The result of operation" a or c "is equal to b".

+3
source share
Answer

@undur_gongor is absolutely correct. Just add if you are working with arrays, for example:

 a = [1,2,3] c = [4,5,6] b = [5,6] if [a, c].include? b # won't work if your desired result is true end 

You will need to do something like:

 if [a,c].any?{ |i| (i&b).length == b.length } # assuming that you're always working with arrays end # otherwise .. if [a,c].any?{ |i| ([i].flatten&[b].flatten).length == [b].flatten.length } # this'll handle objects other than arrays too. end 
0
source share

How about this? if [a, c] .index (b)! = nil; puts "b = a or b = c"; end

-one
source share

Acolyte pointed out that you can use b == (a||c) , you just used it back, but this only works for the left value, since (a || c) is always a if a is not false.

Another option is to use a ternary operator.

 a==b ? true : b==c 

I am not sure about the speed indicated in the approach to the array, but I think it can be faster since it performs one or two comparisons and does not require processing of arrays. In addition, I assume that this is the same as (a == b || b == c), but it is a stylistic alternative.

-one
source share

All Articles