Ruby 2.4 新特征

Ruby 2.4 新特征 首页 / Ruby入门教程 / Ruby 2.4 新特征

 Ruby核心团队今天发布了新的Ruby版本。 无涯教程将在这里总结Ruby 2.4中一些有趣的新函数。

以前的: Ruby 2.3

Numbers

Fixnum Bignum 已统一为Integer类。

到目前为止,有两个用于存储整数的类- Fixnum(表示小整数), Bignum (表示超出此范围的数字)。 但是,这些是实施细节 程序员在编写代码时无需担心。

这两个类已由单个 Integer 类代替。 以前, Integer 是这两个类的超类, 但现在 Fixnum Bignum 都是Integer

# 2.3
42.class      #=> Fixnum
(2**62).class #=> Bignum

# 2.4
42.class      #=> Integer
(2**62).class #=> Integer

Fixnum == Integer #=> true
Bignum == Integer #=> true

新的Integer#digits方法

42.digits  #=> [2, 4]

浮点修饰符的精度

Float方法,例如#ceil #floor #truncate #round 采用可选参数设置精度。

1.567.round       #=> 2
1.567.round(2)    #=> 1.57
123.456.round(-1) #=> 120

Float#round默认行为保持不变

这不是真的改变, 但是默认行为的这种变化最初使它成为一种预览版本, 后来又恢复了。

默认情况下,#round 使用上舍入行为,即。 1.5将四舍五入为2。 新的行为是使用银行家的四舍五入,将四舍五入到最接近的偶数。 这可能会导致许多现有应用程序中依赖于上舍入四舍五入的错误, 因此保留了原始默认设置。

# suggested behavior
1.5.round  #=> 2
2.5.round  #=> 2

# actual behavior
1.5.round #=> 2
2.5.round #=> 3

Float#round选项

即使恢复了从最近到最近的变化, Float#round 中引入了新选项 允许您显式设置要使用的舍入类型。

2.5.round               #=> 3
2.5.round(half: :even)  #=> 2
2.5.round(half: :down)  #=> 2
2.5.round(half: :up)    #=> 3

binding.irb

无涯教程非常喜欢 binding.pry 方法的pry gem,该方法可以在运行代码时打开REPL。 IRB现在已经引入了此函数,并且当ruby遇到 binding.irb 时,ruby现在会打开一个REPL。

Hash

Hash#compact

此方法以及bang版本的#compact!, 从哈希中删除值为nil的键。

{ a: "foo", b: false, c: nil }.compact
#=> { a: "foo", b: false }

Hash#transform_values

将块应用于哈希中的每个值。 还提供了用于修改现有哈希的#transform_values!方法。 文档中的示例:

h = { a: 1, b: 2, c: 3 }
h.transform_values {|v| v * v + 1 }  #=> { a: 2, b: 5, c: 10 }
h.transform_values(&:to_s)           #=> { a: "1", b: "2", c: "3" }

Strings,Symbols和IO

字符串支持Unicode大小写映射

到目前为止,Ruby仅对ASCII字符执行大小写转换。 String Symbols 现在已扩展为可以使用unicode字符。

# 2.3
"Türkiye".upcase   #=> "TüRKIYE"
"TÜRKİYE".downcase #=> "tÜrkİye"

# 2.4
"Türkiye".upcase   #=> "TÜRKIYE"
"TÜRKİYE".downcase #=> "türki̇ye"

指定字符串缓冲区大小

String.new现在允许capacity参数 指定缓冲区的大小。 这将带来性能上的好处 当字符串将被多次连接时。

String.new('foo', capacity: 1_000)

Symbol#match现在类似于 String#match

Symbol#match用于返回匹配位置, 而 String#match 返回了 MatchData 对象。 这已在2.4中修复,现在都返回 MatchData

# 2.3
:hello_ruby.match(/ruby/) #=> 6

# 2.4
:hello_ruby.match(/ruby/) #=> #<MatchData "ruby">

IO#gets和其他方法会获得断断续续的标志

现在,您可以添加一个可选的 chomp:true 标志到 #gets #readline #each_line #readlines IO.foreach

# In 2.3, you did this
foo = gets.chomp

# 2.4
foo = gets(chomp: true)

正则表达式

Regexp#match?

此新方法返回true或false,而不更新$〜全局变量。 由于它不会创建 MatchData 对象或更新 $〜对象, 它的效果要优于#match

/foo/.match?('foo')  #=> true
$~                   #=> nil

Regexp#named_captures

返回表示有关命名捕获的信息的哈希。

/(?<fname>.+) (?<lname>.+)/.match('Ned Stark').named_captures
#=> {"fname"=>"Ned", "lname"=>"Stark"}

Enumerable

Enumerable#sum

(1..5).sum         #=> 15
%w(a b c).sum('')  #=> "abc"

文件和目录

#empty?方法已添加到 Dir FilePathname

Dir.empty?('path/to/some/dir')     #=> true
File.empty?('path/to/some/file')   #=> true

require 'pathname' # Needed to use Pathname class
Pathname.new('file-or-dir').empty? #=> true

Language特征

在Ruby 2.3中,您会收到语法错误 如果您在条件中尝试了多次分配。 它已改为警告。

# 2.3
if (a,b = [1,2]) then 'yes' else 'no' end
#=> SyntaxError: (irb):9: multiple assignment in conditional

# 2.4

if (a,b = [1,2]) then 'yes' else 'no' end
#=> warning: found = in conditional, should be ==
#=> 'yes'

if (a,b = nil) then 'yes' else 'no' end
#=> warning: found = in conditional, should be ==
#=> 'no'

    祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

    技术教程推荐

    微服务架构核心20讲 -〔杨波〕

    邱岳的产品实战 -〔邱岳〕

    苏杰的产品创新课 -〔苏杰〕

    雷蓓蓓的项目管理实战课 -〔雷蓓蓓〕

    Electron开发实战 -〔邓耀龙〕

    分布式协议与算法实战 -〔韩健〕

    Redis核心技术与实战 -〔蒋德钧〕

    PyTorch深度学习实战 -〔方远〕

    Go进阶 · 分布式爬虫实战 -〔郑建勋〕

    好记忆不如烂笔头。留下您的足迹吧 :)