溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Ruby 2.7有什么新特征

發(fā)布時間:2021-07-06 18:05:34 來源:億速云 閱讀:125 作者:chen 欄目:編程語言

本篇內(nèi)容介紹了“Ruby 2.7有什么新特征”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

自Ruby 2.1.0起, Ruby核心團(tuán)隊每年圣誕節(jié)都發(fā)布了新版本的Ruby, 這次我們得到了2.7.0。 這可能是Ruby的最新2.x版本, 因為Ruby 3.0有望在明年圣誕節(jié)發(fā)布。

這是一個簡短的摘要 此發(fā)行版中一些最有趣的函數(shù)。 (有關(guān)以前的版本,請參閱: 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中使用此函數(shù)的能力。 例如, 我們可以在ActiveRecord中做到這一點:

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

編號參數(shù)

添加了新語法 將編號的參數(shù)添加到塊中。

(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

這些是人為的例子, 但到目前為止,我還不認(rèn)為值得添加這種新語法, 而且我看不出自己在明確的論點上使用了它。

新枚舉和數(shù)組方法

Enumerable#tally 計算一個枚舉中每個項目的出現(xiàn), 并將其作為哈希值返回。

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 結(jié)合了select map合并為一個塊, 避免了中間數(shù)組分配的需要。

# 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 與在數(shù)組上調(diào)用&等效,但是允許多個參數(shù)。

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

Enumerator::Lazy#eager會將惰性枚舉數(shù)轉(zhuǎn)換為非惰性枚舉數(shù)。

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

參數(shù)轉(zhuǎn)發(fā)語法

添加了新語法( ... )以允許 轉(zhuǎn)發(fā)方法的所有參數(shù)。

def add(a, b)
  a + b
end

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

add_and_double(2, 3) #=> 10

Args關(guān)鍵字棄用

在先前的Ruby版本中, 如果您有一個帶有關(guān)鍵字參數(shù)的方法, 并以哈希作為最后一個參數(shù)來調(diào)用它, 哈希將被轉(zhuǎn)換為關(guān)鍵字args。 但是,此方法已被棄用 以期在3.0中刪除此行為。 現(xiàn)在,這將引起警告。

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運算符可用于將哈希轉(zhuǎn)換為關(guān)鍵字args。

foo(**{ key: 2 })

Self調(diào)用 private方法

private的行為已略有變化。 以前,如果您在self上調(diào)用了私有方法, (例如 self.foo ), 將提示 NoMethodError錯誤。 現(xiàn)在通過self 調(diào)用私有方法不再引發(fā)錯誤。

除了添加的所有新函數(shù)之外, 這個版本也很有趣 對于某些沒有切入的函數(shù)。 

引入了管道運算符(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)

之所以被恢復(fù),是因為 Matz和核心團(tuán)隊 想重新考慮更廣泛的設(shè)計 關(guān)于函數(shù)式編程的函數(shù) 可以在Ruby中使用。

“Ruby 2.7有什么新特征”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI