三元运算符 ?: 是否定义为 Ruby 中的方法?
Is the ternary operator ?: defined as a method in Ruby?
我是 Ruby 的新手,对三元运算符 ?:
的工作原理有点困惑。
据书Engineering Software as a Service: An Agile Approach Using Cloud Computing:
every operation is a method call on some object and returns a value.
从这个意义上说,如果三元运算符表示一个操作,那么它就是对一个有两个参数的对象的方法调用。但是,我在Ruby's documentation中找不到三元运算符代表的任何方法。三元运算符是否表示 Ruby 中的运算?书中提到的上述说法是错误的吗? Ruby 中的三元运算符真的只是 if ... then ... else ... end
语句的语法糖吗?
请注意:
我的问题与 有关,但与那个问题不同。我知道如何按照 post 中描述的方式使用三元运算符。我的问题是关于三元运算符在 Ruby 中定义的位置以及三元运算符是否被定义为一个或多个方法。
Is the ternary operator in Ruby really just a syntactic sugar for if ... then ... else ... end statements?
是的。
来自doc/syntax/control_expressions.rdoc
You may also write a if-then-else expression using ?
and :
. This ternary if:
input_type = gets =~ /hello/i ? "greeting" : "other"
Is the same as this if
expression:
input_type =
if gets =~ /hello/i
"greeting"
else
"other"
end
"According to this book, "every operation is a method call on some object and returns a value." In this sense, if the ternary operator represents an operation, it is a method call on an object with two arguments."
if
、unless
、while
和 until
不是运算符,它们是控制结构。它们的修饰符版本出现在 operator precedence table 中,因为它们需要具有优先级才能被解析。他们只是检查他们的条件是真还是假。在 Ruby 中这很简单,只有 false
和 nil
是假的。其他一切都是真的。
运算符是 !
、+
、*
和 []
之类的东西。他们是unary or binary。您可以通过对各种对象调用 .methods.sort
来查看它们的列表。例如...
2.4.3 :004 > 1.methods.sort
=> [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :^, :__id__, :__send__, etc...
注意在Smalltalk, from which Ruby borrows heavily, everything really is a method call. Including the control structures.
Is the ternary operator in Ruby really just a syntactic sugar for if ... then ... else ... end
statements?
(另一个)是。
这是 a ? b : c
的解析树:
$ ruby --dump=parsetree -e 'a ? b : c'
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_PRELUDE (line: 1)
# +- nd_head:
# | (null node)
# +- nd_body:
# | @ NODE_IF (line: 1)
# | +- nd_cond:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :a
# | +- nd_body:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :b
# | +- nd_else:
# | @ NODE_VCALL (line: 1)
# | +- nd_mid: :c
# +- nd_compile_option:
# +- coverage_enabled: false
这是 if a then b else c end
的解析树:
$ ruby --dump=parsetree -e 'if a then b else c end'
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_PRELUDE (line: 1)
# +- nd_head:
# | (null node)
# +- nd_body:
# | @ NODE_IF (line: 1)
# | +- nd_cond:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :a
# | +- nd_body:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :b
# | +- nd_else:
# | @ NODE_VCALL (line: 1)
# | +- nd_mid: :c
# +- nd_compile_option:
# +- coverage_enabled: false
它们是相同的。
在许多语言中,?:
是一个 表达式 而 if-then-else
是一个 语句 。在Ruby中,都是表达式。
我是 Ruby 的新手,对三元运算符 ?:
的工作原理有点困惑。
据书Engineering Software as a Service: An Agile Approach Using Cloud Computing:
every operation is a method call on some object and returns a value.
从这个意义上说,如果三元运算符表示一个操作,那么它就是对一个有两个参数的对象的方法调用。但是,我在Ruby's documentation中找不到三元运算符代表的任何方法。三元运算符是否表示 Ruby 中的运算?书中提到的上述说法是错误的吗? Ruby 中的三元运算符真的只是 if ... then ... else ... end
语句的语法糖吗?
请注意:
我的问题与
Is the ternary operator in Ruby really just a syntactic sugar for if ... then ... else ... end statements?
是的。
来自doc/syntax/control_expressions.rdoc
You may also write a if-then-else expression using
?
and:
. This ternary if:input_type = gets =~ /hello/i ? "greeting" : "other"
Is the same as this
if
expression:input_type = if gets =~ /hello/i "greeting" else "other" end
"According to this book, "every operation is a method call on some object and returns a value." In this sense, if the ternary operator represents an operation, it is a method call on an object with two arguments."
if
、unless
、while
和 until
不是运算符,它们是控制结构。它们的修饰符版本出现在 operator precedence table 中,因为它们需要具有优先级才能被解析。他们只是检查他们的条件是真还是假。在 Ruby 中这很简单,只有 false
和 nil
是假的。其他一切都是真的。
运算符是 !
、+
、*
和 []
之类的东西。他们是unary or binary。您可以通过对各种对象调用 .methods.sort
来查看它们的列表。例如...
2.4.3 :004 > 1.methods.sort
=> [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, :<=>, :==, :===, :=~, :>, :>=, :>>, :[], :^, :__id__, :__send__, etc...
注意在Smalltalk, from which Ruby borrows heavily, everything really is a method call. Including the control structures.
Is the ternary operator in Ruby really just a syntactic sugar for
if ... then ... else ... end
statements?
(另一个)是。
这是 a ? b : c
的解析树:
$ ruby --dump=parsetree -e 'a ? b : c'
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_PRELUDE (line: 1)
# +- nd_head:
# | (null node)
# +- nd_body:
# | @ NODE_IF (line: 1)
# | +- nd_cond:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :a
# | +- nd_body:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :b
# | +- nd_else:
# | @ NODE_VCALL (line: 1)
# | +- nd_mid: :c
# +- nd_compile_option:
# +- coverage_enabled: false
这是 if a then b else c end
的解析树:
$ ruby --dump=parsetree -e 'if a then b else c end'
###########################################################
## Do NOT use this node dump for any purpose other than ##
## debug and research. Compatibility is not guaranteed. ##
###########################################################
# @ NODE_SCOPE (line: 1)
# +- nd_tbl: (empty)
# +- nd_args:
# | (null node)
# +- nd_body:
# @ NODE_PRELUDE (line: 1)
# +- nd_head:
# | (null node)
# +- nd_body:
# | @ NODE_IF (line: 1)
# | +- nd_cond:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :a
# | +- nd_body:
# | | @ NODE_VCALL (line: 1)
# | | +- nd_mid: :b
# | +- nd_else:
# | @ NODE_VCALL (line: 1)
# | +- nd_mid: :c
# +- nd_compile_option:
# +- coverage_enabled: false
它们是相同的。
在许多语言中,?:
是一个 表达式 而 if-then-else
是一个 语句 。在Ruby中,都是表达式。