tags:

views:

54

answers:

1

Here is code

def tmp

  a = ancestors.first(ancestors.index(ActiveRecord::Base))

  b = a.sum([]) { |m| m.public_instance_methods(false) | 
                  m.private_instance_methods(false) | 
                  m.protected_instance_methods(false) }

  b.map {|m| m.to_s }.to_set

end

I thought | is bitwise OR operator. So how come b contains non boolean values?

+3  A: 

It would have helped if you said what your code was supposed to do, but I think I finally got it. The | that you have is not an OR at all, neither bitwise nor logical. It is an array union operation. Look it up under Array rubydoc. It takes Array arguments and gives an Array result consisting of all elements that are in either array.

Since pretty much everything in Ruby is an object (aside from blocks, not relevant here), there are no absolute "operators" except for simple assignment. Every operator is in fact a method defined on some class, and therefore contextual.

Also, as someone rightly pointed out (deleted now), bitwise OR deals with integers, not booleans: 7 | 12 == 15. Logical or || deals with boolean values, but it too can return a non-boolean, since strictly speaking everything except for nil and false is true. Thus, 7 || 12 returns 7, not true (which is still equivalent to true in most contexts).

UPDATE: I've overlooked the fact that || and &&, when used on a Boolean object, can not actually be defined in Ruby, because of their short-circuit semantics. This nevertheless does not change the fact that they behave like methods of Boolean.

Amadan
the code is a snippet from rails code. Sorry I should have mentioned that.
Nadal