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.5

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

Share

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

This article mainly explains what are the new features of Ruby 2.5. the content of the explanation is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn what are the new features of Ruby 2.5.What are the new features of Ruby 2.5?

Rescue do/end block

If you need to catch an exception in a block, the only way is to put it in a begin/end block. You can now catch exceptions within all do/end blocks.

(1.. 5) .each do | n | do_something (n) rescue SomeError = > e puts e next end deleted lookup

In previous versions, if Ruby could not find a constant in the current scope, it would fall back to the top-level constant and issue a warning. It was changed in 2.5, which now results in an exception.

Class Foo; endclass Bar; end# Ruby 2.4Foo::Bar# warning: toplevel constant Bar referenced by Foo::Bar#= > Bar# Ruby 2.5Foo::Bar# NameError backtracking in reverse order

When printing to the console, Ruby displays backtracking in reverse order. You will also see the error message in bold and underlined. This is an experimental function, and the order does not change when printing to the log.

When working on a Rails project, you usually need to scroll back a long way to find the line in the backtracking that shows the line where the exception occurred. By reversing backtracking, you don't have to scroll backwards to get context on the line that caused the exception.

Kernel#yield_self

This method passes the object to the block and returns the value returned by the block.

Isbn = "0306-40615-2" isbn.gsub ('-',''). Self _ self {| isbn | URI ("# {API_URL}? q=isbn:# {isbn}")} .room_ self {| uri | Net:HTTP.get (uri)} .room_ self {| json_response | JSON.parse (json_response)} .room_ self {| response | response.dig ('items',' volumeInfo')}

There are a lot of people in the Ruby community who are interested in Elixir, so this may eventually become a popular function.

String method

String#-@: deduplicates unfrozen strings.

S =-'foo' # = >' foo's.frozen? # = > true

Delete_prefix and delete_suffix (and bang! Version)

'Mr. Smith'.delete_prefix (' Mr.') # = > 'Smith'' Wellington St.'.delete_suffix ('St.') # = >' Wellington'

Grapheme_clusters

S = "a\ u0300" # = > "a neighbor" s.grapheme_clusters # = > ["asides"] s.codepoints # = > [97,768] array method

Added # append and # prepend methods, aliases for # push and # unshift, respectively. These aliases have been introduced in Rails through ActiveSupport, but it's nice to see them introduced into Ruby.

List = ['baked,' c'] list.prepend ('a') # = > ['baked,' baked,'c'] list.append ('d') # = > ['axed,' baked, 'cased,' d'] list # = > ['axed,' baked, 'cased,' d'] hash method

Added # slice and # transform_keys, also originally from ActiveSupport.

H = {a: 1, b: 2, c: 3, d: 4} h.slice (: a,: B) # = > {a: 1, b: 2} h.transform_keys (&: to_s) # = > {"a" = > 1, "b" = > 2, "c" = > 3, "d" = > 4} Goodbye, ubygems.rb!

This change will have zero impact on the way you use Ruby, but I'm curious about why we have a ubygems.rb file in stdlib, so I want to mention it because it has been deleted.

Ruby has a-r flag that allows you to need a library, such as ruby-rmath to require 'math'. The name of the rubygems file is ubygems.rb so that the flag can be used as-rubygems instead of-rrubygems. This file has not been needed since Ruby 1.9 and has now been removed from stdlib.

Thank you for your reading, the above is the content of "what are the new features of Ruby 2.5". After the study of this article, I believe you have a deeper understanding of what the new features of Ruby 2.5 have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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