Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the new features of Ruby 2.3

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "what are the new features of Ruby 2.3". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of Ruby 2.3.

Let's see which new functions have been introduced.

# Install using RVMrvm install 2.3.The Using Rbenvbrew upgrade ruby-build-- HEADrbenv install 2.3.0Safe navigation operator

A new operator (&.) has been introduced. It can be useful in cases where you need to check whether the object is nil before calling a method on the object. If the object is equal to nil, it returns nil, otherwise the method of the object is called.

# Ruby = b, it is checking whether all key-value pairs in b also exist in a.

{x: 1, y: 2} > = {x: 1} # = > true {x: 1, y: 2} > = {x: 2} # = > false {x: 1} > = {x: 1, y: 2} # = > false

In the first example above, the key-value pair [: XMagne1] in RHS is the code in LHS-[: XMagne1], [: YPhone2]], so it returns true.

This also applies to all other comparison operators. The Olivier Lacan of this function is proposed in Ruby, and a good explanation of the hash comparison in Ruby 2.3 is written.

Hash#to_proc

Hash#to_proc returns a lambda that maps keys to values. When you call lambda with a key, it returns the corresponding value from the hash.

H = {foo: 1, bar: 2, baz: 3} p = h.to_procp.call (: foo) # = > 1p.call (: bar) # = > 2p.call (: quux) # = > nil

It seems useless in itself. Why not use [] to access the element? However, it becomes interesting when we use the & operator to create a proc and pass it to the Enumerable block.

H = {foo: 1, bar: 2, baz: 3} # instead of this: [: foo,: bar] .map {| key | h [key]} # = > [1,2] # we can use this syntax: [: foo,: bar] .map (& h) # = > [1,2] Hash#fetch_values

This method works like Hash#values_at-it gets the value corresponding to the list of keys we passed in. The difference is that when the key does not exist, # values_at returns nil, while # fetch_values throws a KeyError for the undisplayed key.

H = {foo: 1, bar: 2, baz: 3} h.fetch_values (: foo,: bar) # = > [1,2] h.values_at (: foo,: quux) # = > [1, nil] h.fetch_values (: foo,: quux) # = > raise KeyErrorEnumerable#grep_v

The grep_v method is equivalent to the-v option in the command line grep utility. It returns a list of items that do not meet the criteria.

List =% w (foo bar baz) list.grep_v (/ ba/) # = > ['foo'] list.grep (/ ba/) # = > [' bar', 'baz'] so far, I believe you have a better understanding of "what are the new features of Ruby 2.3". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report