本文简要介绍ruby语言中  Integer.self[offset]  的用法。
用法
self[offset] → 0 or 1self[offset, size] → integerself[range] → integer从 self 返回一个位片。
使用参数 offset ,返回给定偏移量的位,其中偏移量 0 指的是最低有效位:
n = 0b10 # => 2
n[0]     # => 0
n[1]     # => 1
n[2]     # => 0
n[3]     # => 0原则上,n[i] 等价于 (n >> i) & 1 。因此,负索引总是返回零:
255[-1] # => 0使用参数 offset 和 size ,从 self 返回 size 位,从 offset 开始并包括更重要的位:
n = 0b111000       # => 56
"%010b" % n[0, 10] # => "0000111000"
"%010b" % n[4, 10] # => "0000000011"使用参数 range ,从 self 返回 range.size 位,从 range.begin 开始并包括更重要的位:
n = 0b111000      # => 56
"%010b" % n[0..9] # => "0000111000"
"%010b" % n[4..9] # => "0000000011"如果无法构造切片,则引发异常。
相关用法
- Ruby Integer.self >=用法及代码示例
- Ruby Integer.self >>用法及代码示例
- Ruby Integer.self ** numeric用法及代码示例
- Ruby Integer.self <=>用法及代码示例
- Ruby Integer.self % other用法及代码示例
- Ruby Integer.self / numeric用法及代码示例
- Ruby Integer.self * numeric用法及代码示例
- Ruby Integer.self < other用法及代码示例
- Ruby Integer.self & other用法及代码示例
- Ruby Integer.self ^ other用法及代码示例
- Ruby Integer.self >用法及代码示例
- Ruby Integer.self | other用法及代码示例
- Ruby Integer.self ==用法及代码示例
- Ruby Integer.self <=用法及代码示例
- Ruby Integer.self << count用法及代码示例
- Ruby Integer.self - numeric用法及代码示例
- Ruby Integer.self + numeric用法及代码示例
- Ruby Integer.sqrt用法及代码示例
- Ruby Integer.succ用法及代码示例
- Ruby Integer.size用法及代码示例
- Ruby Integer.nobits?用法及代码示例
- Ruby Integer.next用法及代码示例
- Ruby Integer.truncate用法及代码示例
- Ruby Integer.bit_length用法及代码示例
- Ruby Integer.floor用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Integer.self[offset]。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
