Ruby Ruby 2.7 的新功能

ericguo · December 06, 2019 · Last by ericguo replied at January 12, 2020 · 6110 hits

What’s new in Ruby 2.7?

  • 个人粗粗看了一下,比较喜欢 filter_map 和 tally;
  • Argument Forwarding 运算符(...)其实不是很喜欢,主要是和 js 的...语义太不一样了;
  • Pattern Matching,似乎就是照抄 Elixir 的?那就是支持!
  • Numbered Parameters,我不知道你们怎么看,反正我不会用的,我情愿多打点字。

Numbered Parameters

似乎和 Swift 的Shorthand Arguments有异曲同工之妙,以后可以少写一些双坚线||

Elixir 的 Pattern Matching 还不是合一

tally 确实挺强大,少些好多代码。

我倒是挺喜欢 Numbered Parameters,以前就在想怎么把[1, 2].map { |ele| ele + 1 }写短一点

越弄越复杂,做一件事情又多了一件方法

都是些无关痛痒的东西

这是在憋大招的节奏啊

看了 Numbered Parameters 的例子感觉可读性更差了,我不打算用,不反对别人用,只要不需要我维护。

以后 ruby style guide 里又会多一条:不要使用 Numbered Parameters。

我都是使用 each_with_object 实现 filter_map 功能的😀

# Ruby 2 delegation
def foo(*args, &blk)
  bar(*args, &blk)
end

# Ruby 3 delegation
def foo(*args, **opt, &blk)
  bar(*args, **opt, &blk)
end

# Delegation using (...) (works on both Ruby 2.7 and Ruby 3 or later)
def foo(...)
  bar(...)
end

原来 Argument Forwarding 运算符(...)是为了解决 keyword argument separation 的 问题而引入的,Ruby 语法真心复杂。。 https://sourcediving.com/ruby-2-7-news-commentary-by-cookpads-full-time-ruby-comitters-bdbaacb36d0c

You need to Sign in before reply, if you don't have an account, please Sign up first.