源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

详解Ruby当中的算数运算

  • 时间:2020-04-05 07:34 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:详解Ruby当中的算数运算
 Ruby支持一系列丰富的运算符的一个现代化的语言。大多数运算符实际上是方法调用。例如,a + b的被解释为a, +(b)变量引用的对象的方法被称为一个用b作为它的参数。 对于每个运算符 (+ - * / % ** & | ^ << >> && ||), 有相应的赋值运算符缩写形式 (+= -= 等) [b]Ruby算术运算符:[/b] 假设变量a=10,变量b=20: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100122_0_54680.jpg[/img] [b] Ruby比较操作符:[/b] 假设变量a=10,变量b=20: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100122_1_70261.jpg[/img] [b] Ruby赋值运算符:[/b] 假设变量a=10,变量b=20: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100123_2_9219.jpg[/img] [b] Ruby并行赋值:[/b] Ruby还支持并行赋值的变量。这使得多个一行Ruby代码来初始化变量。例如:
a = 10
b = 20
c = 30

需要更迅速声明,使用并行赋值:
a, b, c = 10, 20, 30

并行赋值交换两个变量的值也是有用的:
a, b = b, c

[b]Ruby位运算符:[/b] 位运算符位和位操作执行位。 假设当a =60和b=13;现在以二进制格式将如下:
a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

[b]支持Ruby语言的位运算符 [/b] [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100123_3_97080.jpg[/img] [b] Ruby逻辑运算符:[/b] 支持Ruby语言的逻辑运算符 假设变量a=10,变量b=20: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100124_4_57841.jpg[/img] [b] Ruby三元运算符:[/b] 还有一个运算符称为三元运算符。这首先计算一个表达式为true或false值,然后执行一个计算结果来决定两个语句的哪一个。条件运算符的语法如下: [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100124_5_23377.jpg[/img] [b] Ruby范围运算符:[/b] Ruby中的序列范围是用来创建连续值 - 组成了开始值/结束值,并在两者之间的值的范围内。 在Ruby中,创建这些序列使用“..”和“...”范围运算符。这两个点的形式建立一个包容性的范围,而三个点的形式创建了一个范围,不包括指定的最大值。 [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100124_6_89022.jpg[/img] [b] Ruby defined? 操作符:[/b] defined是一个特殊的操作符采取的形式的方法调用,以确定是否通过表达式定义。如果没有被定义的表达式,它返回一个描述字符串求解出的表达式或nil 有很多种用法 defined操作符: 用法 1
defined? variable # True if variable is initialized

例如 :
foo = 42
defined? foo  # => "local-variable"
defined? $_   # => "global-variable"
defined? bar  # => nil (undefined)

用法 2
defined? method_call # True if a method is defined

例如 :
defined? puts    # => "method"
defined? puts(bar)  # => nil (bar is not defined here)
defined? unpack   # => nil (not defined here)

用法 3
# True if a method exists that can be called with super user
defined? super

例如 :
defined? super   # => "super" (if it can be called)
defined? super   # => nil (if it cannot be)

用法 4
defined? yield  # True if a code block has been passed

例如 :
defined? yield  # => "yield" (if there is a block passed)
defined? yield  # => nil (if there is no block)

[b]Ruby "." 双冒号 "::" 运算符:[/b] 调用一个模块方法,通过模块的名称和句点它的名字前,引用一个常数使用该模块的名称和两个冒号。 ::使得一元运算符,常数,实例方法和类方法在类或模块定义,从任何地方访问外的类或模块。 请记住:在Ruby中,类和方法可以被视为常数。只需要前缀::Const_name的表达式返回相应的类或模块对象。 如果没有前缀表达式时,主要对象类默认情况下使用。 这里有两个例子:
MR_COUNT = 0    # constant defined on main Object class
module Foo
 MR_COUNT = 0
 ::MR_COUNT = 1  # set global count to 1
 MR_COUNT = 2   # set local count to 2
end
puts MR_COUNT    # this is the global constant
puts Foo::MR_COUNT # this is the local "Foo" constant

Second Example:

CONST = ' out there'
class Inside_one
  CONST = proc {' in there'}
  def where_is_my_CONST
   ::CONST + ' inside one'
  end
end
class Inside_two
  CONST = ' inside two'
  def where_is_my_CONST
   CONST
  end
end
puts Inside_one.new.where_is_my_CONST
puts Inside_two.new.where_is_my_CONST
puts Object::CONST + Inside_two::CONST
puts Inside_two::CONST + CONST
puts Inside_one::CONST
puts Inside_one::CONST.call + Inside_two::CONST

[b]Ruby运算符优先级[/b] 下表列出了所有运算符从最高优先级到最低。 [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100125_7_49908.jpg[/img] 注: 方法列一个是运算符实际上是方法,因此可能会被改写。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部