Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the new features of Ruby 2.3

Shulou Source: shulou.com Published: 2022-06-03 06:12:03 10月02日 Update

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!

Tags: Methods characters strings functions objects useful hash variable feature name file operator operation utility element content case array text version Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft OPPO Reno Shulou Information MariaDB