Ruby中的'&'和'&'有什么区别

时间:2022-03-28 22:57:32

Recently, I observed a very interesting result in Ruby while making use of && and & for the input combination of 0 & 1.

最近,我在Ruby中观察到一个非常有趣的结果,在使用&&和&进行0和1的输入组合时。

Can someone please explain the below output with respect to the above mentioned two operators? The below is implemented using Ruby 2.0.0-p451

是否有人可以解释一下上面提到的两个操作符的输出?下面是使用Ruby 2.0 -p451实现的

2.0.0-p451 :006 > 0 && 1
 => 1 


2.0.0-p451 :008 > 0 & 1
 => 0 

Thank you

谢谢你!

5 个解决方案

#1


6  

&& is a boolean and. It returns the second argument if the first argument is true-ish. Because 0 is true-ish in Ruby, 1 is returned.

&&是一个布尔值。如果第一个参数是真的,它返回第二个参数。因为0在Ruby中是真实的,所以返回1。

& is a bitwise and. It compares the bit representation of the values. Because (imaging 8 bit) 00000000 (0) and 00000001 (1) have no 1 digits in common, 00000000 (0) is returned.

而且是位位。它比较值的位表示。因为(成像8位)00000000(0)和00000001(1)没有相同的1位,所以返回00000000(0)。

#2


4  

&& is the logical AND operator. It will be truthy, IFF both operands are truthy. And it is lazy (aka short-circuiting), which means it will stop evaluating as soon as the result has been fully determined. (So, since both operands need to be truthy, if the first operand is falsy, you already know that the result is going to be falsy, without even evaluating the second operand.) It will also not just return true or false, but rather return the operand which determines the outcome. IOW: if a is falsy, it'll return a, otherwise it'll return b:

&&是逻辑运算符。这将是真实的,如果两个操作数都是真实的。它是懒惰的(又称短路),这意味着一旦结果完全确定,它就会停止计算。(既然两个操作数都必须是真实的,如果第一个操作数是假的,你已经知道结果是假的,甚至不计算第二个操作数)它不仅返回true或false,还返回决定结果的操作数。如果a是假的,它会返回a,否则它会返回b:

nil && (loop {})
# => nil
# Note: the infinite loop is *not* evaluated
# Note: the return value is nil, not false

true && nil
# => nil

true && 'Hello'
# => 'Hello'

& simply calls the method &. It will do whatever the object wants it to do:

简单地调用方法&。它会做物体想做的任何事情:

def (weirdo = Object.new).&(other)
  puts 'Whoah, weird!'
  'Hello, ' + other
end

weirdo & 'World'
# Whoah, weird!
# => 'Hello, World'

In general, & and its brother | are expected to perform conjunction and disjunction. So, for booleans, they are perform AND and OR (TrueClass#&, FalseClass#&, NilClass#&, TrueClass#|, FalseClass#|, NilClass#|) with the exception that & and | are standard method calls and thus always evaluate their argument and that they always return true or false and not their arguments.

一般来说,&和它的兄弟|被期望执行连接和分离。因此,对于布尔值,它们是AND和OR (TrueClass#&, FalseClass#&, NilClass#&, TrueClass#|, FalseClass#|, NilClass#|),唯一的例外是&和|是标准的方法调用,因此总是评估它们的参数,它们总是返回真或假,而不是它们的参数。

For Sets, they perform set intersection and set union: Set#&, Set#|. For other collections (specifically Arrays), they also perform set operations: Array#&, Array#|.

对于集合,它们执行集合交集并设置union: set #&, set #|。对于其他集合(特别是数组),它们还执行set操作:Array#&, Array#|。

For Integers, they perform BITWISE-AND of the two-complement's binary representation: Fixnum#&, Bignum#&, Fixnum#|, Bignum#|.

对于整数,它们执行bitwise -和二进制表示的二进制表示:Fixnum#&, Bignum#&, Fixnum#|, Bignum#|。

#3


0  

This page gives a good explanation of the different operators in Ruby.

这个页面很好地解释了Ruby中不同的操作符。

&& is logical AND operator in ruby.

&&是ruby中的逻辑运算符。

> a = true
> b = true
> c = false
> a && b
 => true 
> a && c
 => false 

& is the bitwise AND operator in ruby. Per the wikipedia article on the description for the "bitwise AND operator":

&是ruby中的位和运算符。*上关于“位和操作员”描述的文章:

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0.

一个位,取两个长度相等的二进制表示,并对每对对应的位执行逻辑和操作。如果第一个位是1,第二个位是1,那么每个位置的结果都是1;否则,结果为0。

#4


0  

&& is logical and. Which is to say, a && b returns true if a is true and b is true. & is bitwise and. In (almost) any other language, logical and of 0 and 1 would be 0, because (almost) all other languages consider 0 to be false. But in ruby, anything except nil and false are considered false.

& &是逻辑。也就是说,如果a为真,b为真,a & b返回真。&是位和。在(几乎)任何其他语言中,逻辑的0和1都是0,因为(几乎)所有其他语言都认为0是假的。但是在ruby中,除了nil和false之外的任何东西都被认为是假的。

#5


0  

One is a boolean operator, the other is a bitwise operator:

一个是布尔运算符,另一个是位运算符:

  # Bitwise operators
  a = 78      #  01001110
  b = 54      #  00110110
  puts (a&b)  #  00000110 = 6
  puts (a|b)  #  01111110 = 126
  puts (a^b)  #  01111000 = 120
  puts (~a)   #  10110001 = -79
  puts (a<<2) #  00111000 = 312
  puts (a>>2) #  00010011 = 19

http://www.public.traineronrails.com/courses/ruby/pages/008-rubyoperators.html

http://www.public.traineronrails.com/courses/ruby/pages/008-rubyoperators.html

#1


6  

&& is a boolean and. It returns the second argument if the first argument is true-ish. Because 0 is true-ish in Ruby, 1 is returned.

&&是一个布尔值。如果第一个参数是真的,它返回第二个参数。因为0在Ruby中是真实的,所以返回1。

& is a bitwise and. It compares the bit representation of the values. Because (imaging 8 bit) 00000000 (0) and 00000001 (1) have no 1 digits in common, 00000000 (0) is returned.

而且是位位。它比较值的位表示。因为(成像8位)00000000(0)和00000001(1)没有相同的1位,所以返回00000000(0)。

#2


4  

&& is the logical AND operator. It will be truthy, IFF both operands are truthy. And it is lazy (aka short-circuiting), which means it will stop evaluating as soon as the result has been fully determined. (So, since both operands need to be truthy, if the first operand is falsy, you already know that the result is going to be falsy, without even evaluating the second operand.) It will also not just return true or false, but rather return the operand which determines the outcome. IOW: if a is falsy, it'll return a, otherwise it'll return b:

&&是逻辑运算符。这将是真实的,如果两个操作数都是真实的。它是懒惰的(又称短路),这意味着一旦结果完全确定,它就会停止计算。(既然两个操作数都必须是真实的,如果第一个操作数是假的,你已经知道结果是假的,甚至不计算第二个操作数)它不仅返回true或false,还返回决定结果的操作数。如果a是假的,它会返回a,否则它会返回b:

nil && (loop {})
# => nil
# Note: the infinite loop is *not* evaluated
# Note: the return value is nil, not false

true && nil
# => nil

true && 'Hello'
# => 'Hello'

& simply calls the method &. It will do whatever the object wants it to do:

简单地调用方法&。它会做物体想做的任何事情:

def (weirdo = Object.new).&(other)
  puts 'Whoah, weird!'
  'Hello, ' + other
end

weirdo & 'World'
# Whoah, weird!
# => 'Hello, World'

In general, & and its brother | are expected to perform conjunction and disjunction. So, for booleans, they are perform AND and OR (TrueClass#&, FalseClass#&, NilClass#&, TrueClass#|, FalseClass#|, NilClass#|) with the exception that & and | are standard method calls and thus always evaluate their argument and that they always return true or false and not their arguments.

一般来说,&和它的兄弟|被期望执行连接和分离。因此,对于布尔值,它们是AND和OR (TrueClass#&, FalseClass#&, NilClass#&, TrueClass#|, FalseClass#|, NilClass#|),唯一的例外是&和|是标准的方法调用,因此总是评估它们的参数,它们总是返回真或假,而不是它们的参数。

For Sets, they perform set intersection and set union: Set#&, Set#|. For other collections (specifically Arrays), they also perform set operations: Array#&, Array#|.

对于集合,它们执行集合交集并设置union: set #&, set #|。对于其他集合(特别是数组),它们还执行set操作:Array#&, Array#|。

For Integers, they perform BITWISE-AND of the two-complement's binary representation: Fixnum#&, Bignum#&, Fixnum#|, Bignum#|.

对于整数,它们执行bitwise -和二进制表示的二进制表示:Fixnum#&, Bignum#&, Fixnum#|, Bignum#|。

#3


0  

This page gives a good explanation of the different operators in Ruby.

这个页面很好地解释了Ruby中不同的操作符。

&& is logical AND operator in ruby.

&&是ruby中的逻辑运算符。

> a = true
> b = true
> c = false
> a && b
 => true 
> a && c
 => false 

& is the bitwise AND operator in ruby. Per the wikipedia article on the description for the "bitwise AND operator":

&是ruby中的位和运算符。*上关于“位和操作员”描述的文章:

A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 and the second bit is 1; otherwise, the result is 0.

一个位,取两个长度相等的二进制表示,并对每对对应的位执行逻辑和操作。如果第一个位是1,第二个位是1,那么每个位置的结果都是1;否则,结果为0。

#4


0  

&& is logical and. Which is to say, a && b returns true if a is true and b is true. & is bitwise and. In (almost) any other language, logical and of 0 and 1 would be 0, because (almost) all other languages consider 0 to be false. But in ruby, anything except nil and false are considered false.

& &是逻辑。也就是说,如果a为真,b为真,a & b返回真。&是位和。在(几乎)任何其他语言中,逻辑的0和1都是0,因为(几乎)所有其他语言都认为0是假的。但是在ruby中,除了nil和false之外的任何东西都被认为是假的。

#5


0  

One is a boolean operator, the other is a bitwise operator:

一个是布尔运算符,另一个是位运算符:

  # Bitwise operators
  a = 78      #  01001110
  b = 54      #  00110110
  puts (a&b)  #  00000110 = 6
  puts (a|b)  #  01111110 = 126
  puts (a^b)  #  01111000 = 120
  puts (~a)   #  10110001 = -79
  puts (a<<2) #  00111000 = 312
  puts (a>>2) #  00010011 = 19

http://www.public.traineronrails.com/courses/ruby/pages/008-rubyoperators.html

http://www.public.traineronrails.com/courses/ruby/pages/008-rubyoperators.html