Ruby 2.7 新特征

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

自Ruby 2.1.0起, Ruby核心团队每年圣诞节都发布了新版本的Ruby, 这次无涯教程得到了2.7.0。 这可能是Ruby的最新2.x版本, 因为Ruby 3.0有望在明年圣诞节发布。

这是一个简短的摘要 此发行版中一些最有趣的函数。 (有关以前的版本,请参阅: 2.6 2.5 2.4 2.3 )。

模式匹配

引入了新的模式匹配语法, 它允许您编写如下的case语句:

def test_pattern(value)
  case value
  in 0
    'Zero'
  in Integer if value < 0
    'Negative'
  in Integer => n
    "Number #{n}"
  in { value: x }
    "Hash value: #{x}"
  in _
    'Unknown'
  end
end

test_pattern(-1)            #=> 'Negative'
test_pattern(0)             #=> 'Zero'
test_pattern(2)             #=> 'Number: 2'
test_pattern({ value: 3 })  #=> 'Hash value: 3'
test_pattern('four')        #=> 'Unknown'

无起点范围

在2.6中,获得了无限范围, 这次将有无起点的范围。 这将允许使用如下语法:

case n
when ..0  then 'Negative'
when 1..9 then 'Single digit'
when 10.. then 'Two or more'
end

另一个有趣的用途 将是在DSL中使用此函数的能力。 例如, 可以在ActiveRecord中做到这一点:

Task.where(due_on: ..Date.today)

编号参数

添加了新语法 将编号的参数添加到块中。

链接:https://www.learnfk.comhttps://www.learnfk.com/ruby/ruby-2-7-features.html

来源:LearnFk无涯教程网

(1..3).map { _1 * _1 } #=> [1, 4, 9]

hash = { a: 2, b: 3 }
hash.map { "#{_1} = #{_2}" } #=> ["a = 1", "b = 2"]

add_3 = -> { _1 + _2 + _3 }
add_3.call(3, 5, 7) #=> 15

新枚举和数组方法

Enumerable#tally 计算一个枚举中每个项目的出现, 并将其作为哈希值返回。

strings = ['a', 'a', 'a', 'b', 'c']
strings.tally
#=> { 'a' => 3, 'b' => 1, 'c' => 1 }

# in 2.6, we'd do it like this:
strings.each_with_object({}) do |value, result|
  result[value] ||= 0
  result[value] += 1
end

Enumerable#filter_map 结合了select map合并为一个块, 避免了中间数组分配的需要。

# squares of odd numbers in 1..10
(1..10).filter_map { |x| x*x if x.odd? } #=> [1, 9, 25, 49, 81]

# ruby 2.6
(1..10).select(&:odd?).map { |x| x*x }

Array#intersection 与在数组上调用&等效,但是允许多个参数。

[1,2,3,4,5].intersection([2,3,4], [2,4,5]) #=> [2, 4]

Enumerator::Lazy#eager会将惰性枚举数转换为非惰性枚举数。

(1..).lazy
  .select(&:odd?)    # lazy
  .take(3)           # lazy
  .eager             # next operation will be non-lazy
  .map { |x| x * x } #=> [1, 9, 25]

参数语法

添加了新语法( ... )以允许 转发方法的所有参数。

def add(a, b)
  a + b
end

def add_and_double(...)
  add(...) * 2
end

add_and_double(2, 3) #=> 10

Args关键字弃用

在先前的Ruby版本中, 如果您有一个带有关键字参数的方法, 并以哈希作为最后一个参数来调用它, 哈希将被转换为关键字args。 但是,此方法已被弃用 以期在3.0中删除此行为。 现在,这将引起警告。

def foo(key: 1)
  key
end

foo({ key: 2 })
#=> 43
# warning: The last argument is used as the keyword parameter
# warning: for `foo' defined here

splat运算符可用于将哈希转换为关键字args。

foo(**{ key: 2 })

Self调用 private方法

private的行为已略有变化。 以前,如果您在self上调用了私有方法, (例如 self.foo ), 将提示 NoMethodError错误。 现在通过self 调用私有方法不再引发错误。

除了添加的所有新函数之外, 这个版本也很有趣 对于某些没有切入的函数。 

引入了管道运算符(pipeline), 如下示例

# with pipeline
foo
  |> bar 1, 2
  |> display

# normal method chaining
foo
  .bar(1, 2)
  .display

引入了方法参考运算符(method reference) 然后删除。 这本来可以写作 File.method(:read)作为 File.:read

some_uri
  .then(&Net::HTTP.:get)
  .then(&JSON.:parse)

之所以被恢复,是因为 Matz和核心团队 想重新考虑更广泛的设计 关于函数式编程的函数 可以在Ruby中使用。

链接

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

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

机器学习40讲 -〔王天一〕

快速上手Kotlin开发 -〔张涛〕

如何设计一个秒杀系统 -〔许令波〕

TypeScript开发实战 -〔梁宵〕

程序员的测试课 -〔郑晔〕

手把手带你写一个Web框架 -〔叶剑峰〕

深入浅出可观测性 -〔翁一磊〕

AI大模型企业应用实战 -〔蔡超〕

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