Ruby TrueClass Single Tube

From the documentation: http://ruby-doc.org/core-2.2.0/TrueClass.html#method-i-7C

true | puts("or") true || puts("logical or") # produces: or 
  • Could you explain when "one channel" is useful?
  • Who cares?

(only in TrueClass context (not Array or Fixnum )

+6
source share
2 answers

This is useful when you do not need a quick operator assessment or .

For example, if you have some methods that do something useful and return true / false as a result and have a different method that should be called only if any of these methods returns true , it is useful to use | :

 def action1 # do something, returns true/false end def action2 # do something, returns true/false end def result_action # do something end result_action if action1 | action2 

If you use boolean || and then if action1 returns true, action2 will not be called ( result_action will be called though)

+4
source

It's hard to say why TrueClass has #| method .

The fact that this is a method means that both its operands are evaluated and then combined, so the string "or" is output. The double pipe is a special design: it will not evaluate the second operand if the first was true. Therefore, as a way to perform logical calculations, a single pipe looks useless.

Now the statement makes a lot more sense on Fixnum : it does the same bitwise OR as it does in C, Java, etc.

For instance:

 >> 133|243 => 247 

Now Java for some reason overloads | in booleans as an operator without a short circuit. Perhaps Ruby makes me too? It doesn't look like Ruby wants to copy Java here.

Most likely the case, because

 true | e 

estimated as

 e 

for any e, Ruby allows you to combine a bunch of truthful expressions. Maybe,

 true | e1 | e2 | e3 | e4 

looks colder than

 e1 e2 e3 e4 true 

or even

 e1; e2; e3; e4; true 

Another possibility may be that it allows you to combine Boolean producing expressions with side effects.

 f(x1) | f(x2) | f(x3) | f(x4) 

and return if any of the created functions were true . Here's a far-fetched example:

 >> def f(x);puts x;return x==2;end => :f >> f(1) || f(2) || f(3) || f(4) 1 2 => true >> f(1) | f(2) | f(3) | f(4) 1 2 3 4 => true 

Of course, this is still just a lame attempt, because you get the same effect:

 >> [f(1),f(2),f(3),f(4)].any? 1 2 3 4 => true 

I suspect, but not 100% sure, that the operator is included in some kind of "completeness" in an algebraic sense. Boolean algebra has AND or OR, and || actually not a method in the classical sense that has an impatient semantics of evaluation. Therefore, perhaps this was chosen because of this reason, and if any programmer finds use for it, then it’s wonderful. But I have never seen, over the years of programming, any pragmatic reason for not shorting.

I would actually argue that if someone were to write code that depends on evaluating the second argument in a boolean context (i.e. using #| ), then such code would be confused --- and, of course, not by reference transparent because it will rely on side effects --- and therefore needs to be rewritten.

+6
source

Source: https://habr.com/ru/post/980172/


All Articles