红宝石&&和 = 运营商误解 [英] Ruby && and = operators misudnerstanding

查看:50
本文介绍了红宝石&&和 = 运营商误解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你认为下一个 Ruby 表达式的结果是什么?

What do you think would be the result of the next expression in Ruby?

    a = 10 && b = 25

尝试在 ming 中计算,然后才使用 irb.因此,如果我们查看有关 Operators Precedence<的 Ruby 文档/a> 然后我们将看到 && 运算符的优先级高于 =.所以你一定认为 Ruby 会以下面的方式计算表达式:

Try to calculate in the ming and only then use irb. So, if we take a look at the Ruby documentation about Operators Precedence then we will se that && operator has a higher priority than =. So you must think that Ruby will evaluate the expression in the next way:

    a = ((10 && b) = 25)

但 Ruby 以另一种方式工作:

But Ruby does a job in another way:

    a = (10 && (b = 25))
    # => 25

所以,b = 25=的优先级更高,然后是&&.谁能解释一下为什么会这样?

So, the priority of the = in b = 25 is higher, then &&. Can anybody explain why it is happend like so?

推荐答案

这与 Ruby 识别裸词标识符

当遇到像a这样的标识符时,它必须通过检查它是关键字、局部变量还是方法来解决它强>按这个顺序.

When it comes across an identifier like a, it has to resolve it by checking to see if it is a keyword, local variable or method in that order.

  • keyword - 如果 a 是关键字,则将其用作关键字
  • local variable - a 右边是否有等号,那么它一定是一个局部变量.如果没有,请检查是否定义了任何局部变量 a.这几点非常重要,这就是为什么不显式调用self就不能在实例方法中调用writer方法的原因.
  • keyword - if a is a keyword then use it as a keyword
  • local variable - is there an equal sign to the right of a, then it must be a local variable. If not check if there is any local variable a defined. These points are very important, it is the reason why you can't call writer methods in instance methods without explicitly calling self.

以这段代码为例

class Person
  attr_accessor :name #creates both attr_reader name & attr_writer name=(text)

  def print_name
    name # => attr_reader method name is called and returns nil

    name = 'Bola'#even though we have the name= method, it doesn't get called
    #what happens is a local variable name is created instead
    #this is as a result of how ruby interpreted the bareword identifier name
    #not a keyword but has an equal sign so must be a local variable

    name # this time local variable is used instead of method because it is resolved first
  end
end

  • method 如果它没有被解析为关键字或局部变量,那么它会假定它是一个方法并尝试调用它.
    • method if it's not resolved as a keyword or local variable then it assumes it's a method and tries to call it.
    • 这就是代码的评估方式

      • && 10 左边是什么,它可以理解
      • 10&& 右侧的内容进行比较,即 b 但它必须评估什么 b 所以它使用我上面描述的过程来解决它,结果是局部变量 b = 25.赋值操作总是返回其右侧的值,即 25
      • 比较 1025 返回 25
      • 最后一个 = 25
      • what is to the left of && 10, which it can make sense of
      • compare that 10 to what is on the right of && which is b but it has to evaluate what b is so it resolves it using the procedure I described above which results in local variable b = 25. Assignment operations always return the values on their right which is 25
      • compare 10 and 25 which returns 25
      • finally a = 25

      话虽如此,&& 确实具有更高的优先级,并且 = 符号具有相同的优先级.希望这个解释清楚.

      With that being said && does have higher priority and the = signs are of the same precedence. Hope this explanation was clear.

      这篇关于红宝石&amp;&amp;和 = 运营商误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆