Ruby 2.7.0 stable release: the introduction of pattern matching, compact GC, improve REPL

Ruby 2.7.0 stable version released on Christmas Day, and this release introduces many new features and performance improvements, the most notable include:

  • Matching pattern (Pattern Matching)
  • REPL improvements
  • Compact GC (Compaction GC)
  • Separation of positional parameters and keyword arguments

Pattern matching (Experimental)

Pattern matching is characteristic of functional programming languages widely used, if a match a pattern, it can traverse a given object and assign its value, is still in the experimental stage  [the Feature # 14912] :

require "json"

json = <<END
{
  "name": "Alice",
  "age": 30,
  "children": [{ "name": "Bob", "age": 2 }]
}
END

case JSON.parse(json, symbolize_names: true)
in {name: "Alice", children: [{name: "Bob", age: age}]}
  p age #=> 2
end

Specific details on this feature, please see  Pattern matching - the Feature New in Ruby 2.7 .

REPL improvements

Interactive environment irb binding now supports multi-line editing, supported by reline, reline readline compatible with a pure Ruby implementation. It also provides rdoc integration. In irb, the references may be displayed to a given class or module methods. Further, source lines, and test results of core classes binding.irb object displayed now is displayed in color-coded.

Compact GC (Compaction GC)

Compact GC can defragment the fragmented memory space. Ruby multithreaded program may lead to memory fragmentation, resulting in high memory usage and slow down. Introduced GC.compact way to compress the heap, this compression function active object in the heap, so you can use fewer pages and heap may be more friendly to CoW.

Separation of positional parameters and keyword arguments

Automatic conversion parameter and a position parameter keyword is marked as obsolete (deprecated), automatic conversion 3 will be removed in Ruby. [Function # 14183]

  • When passing a Hash method as the last argument, or the argument passed no keywords, it will throw a warning. If you need to continue to be treated as keyword parameters, you need to add two asterisks to avoid warnings and ensure proper behavior in Ruby 3.
  def foo(key: 42); end; foo({key: 42})   # warned
  def foo(**kw);    end; foo({key: 42})   # warned
  def foo(key: 42); end; foo(**{key: 42}) # OK
  def foo(**kw);    end; foo(**{key: 42}) # OK
  • When passing a Hash method accepts keyword arguments to a method, but does not deliver sufficient positional parameters, keyword parameters will be considered as the last parameter position, and throw a warning. Set the parameters for the package to avoid the warning and Hash objects in Ruby 3 to ensure proper behavior.
  def foo(h, **kw); end; foo(key: 42)      # warned
  def foo(h, key: 42); end; foo(key: 42)   # warned
  def foo(h, **kw); end; foo({key: 42})    # OK
  def foo(h, key: 42); end; foo({key: 42}) # OK
  • When the method accepts keyword parameters passed, but does not keyword division (splat), and contains both afferent and non-Symbol Symbol's key, then the Hash will be split, but will throw a warning. You need to pass two separate Hash when you call to make sure that in Ruby 3 normal behavior.
  def foo(h={}, key: 42); end; foo("key" => 43, key: 42)   # warned
  def foo(h={}, key: 42); end; foo({"key" => 43, key: 42}) # warned
  def foo(h={}, key: 42); end; foo({"key" => 43}, key: 42) # OK
  • When a keyword does not accept the method, but incoming calls when the keywords, keywords are treated as positional parameters, there will be no warning is thrown. This behavior will continue to work in Ruby 3.
  def foo(opt={});  end; foo( key: 42 )   # OK
  • If the method support any parameters passed, then the non-Symbol will also be allowed as a keyword argument. [Function # 14183]
  def foo(**kw); p kw; end; foo("str" => 1) #=> {"str"=>1}
  • **nil It is allowed to use in the method definition for labeling method does not accept keyword parameters. Keywords parameters to call these methods will throw an ArgumentError  [Function # 14183]
  def foo(h, **nil); end; foo(key: 1)       # ArgumentError
  def foo(h, **nil); end; foo(**{key: 1})   # ArgumentError
  def foo(h, **nil); end; foo("str" => 1)   # ArgumentError
  def foo(h, **nil); end; foo({key: 1})     # OK
  def foo(h, **nil); end; foo({"str" => 1}) # OK
  • Methods empty keyword segmentation (splat) does not accept incoming a keyword will not continue to be treated as an empty Hash, Hash is empty unless necessary as a parameter, and this situation will throw a warning. Remove double asterisk to the Hash passed as positional parameters. [Function # 14183]
  h = {}; def foo(*a) a end; foo(**h) # []
  h = {}; def foo(a) a end; foo(**h)  # {} and warning
  h = {}; def foo(*a) a end; foo(h)   # [{}]
  h = {}; def foo(a) a end; foo(h)    # {}

If you want to disable the "deprecation alert warning (deprecation warnings)", use the command-line parameters -W:no-deprecatedor add Warning[:deprecated] = falseto the code.

Other noteworthy new features

  • The method of reference operators, .:as the experimental features added. Function # 12125 , feature # 13581

  • Experimentally added to the numbers as the default parameter block parameters characteristic. Function # 4475

  • Headless range experimentally added. Although it may not have an infinite range of less useful, but it is very useful for the development of DSL. Function # 14799

  ary[..3]  # identical to ary[0..3]
  rel.where(sales: ..100)
  • Added  Enumerable#tally, it will calculate the number of times each element appears.
  ["a", "b", "c", "b"].tally
  #=> {"a"=>1, "b"=>2, "c"=>1}
  def foo
  end
  private :foo
  self.foo
  a = %w(foo bar baz)
  e = a.lazy.map {|x| x.upcase }.map {|x| x + "!" }.eager
  p e.class               #=> Enumerator
  p e.map {|x| x + "?" }  #=> ["FOO!?", "BAR!?", "BAZ!?"]

Performance Improvements

  • The JIT [experimental]

    • When optimizing hypothesis is unsuccessful, the JIT code may be recompiled to a lower degree of optimization code.

    • When method (Method) is considered to be purely functional (pure), it will be optimized with the method. This optimization method is still experimental, many methods are not considered to be purely functional.

    • --jit-min-calls Default adjusted from 5 to 10,000.

    • --jit-max-cache Default adjusted from 000 to 100.

  • # To_s Symbol Module # name true.to_s false.to_s  和 nil.to_s` now always returns a freeze (frozen) string. The returned string is always equal and given object. [Experimental]  [Function # 16150]

  • CGI.escapeHTML The performance was promoted. GH-2226

Since version 2.6 Other significant changes

  • A few standard library has been updated
    • Bundler 2.1.0.pre.1
    • RubyGems 3.1.0.pre.1
    • CSV 3.1.2 (NEWS)
    • Coll 1.4.15
    • REXML 3.2.3 (NEWS)
    • RSS 0.2.8 (NEWS)
    • StringScanner 1.0.3
    • Some other without the original version of the library has also been updated.
  • Now the block method calls, if  Proc.new and  proc produces no warning block.

  • lambda When the method call if there is no block will generate block error.

  • Emoji and Unicode version of the update from 11.0.0 to 12.0.0. [Function # 15321]

  • Unicode update to version 12.1.0, add support for the new resolution "and to make" U + 32FF's. [Function # 15195]

  • Date.jisx0301, Date#jisx0301 And  Date.parse show support for the new reign as Japan's unofficial extensions until the new JIS X 0301 release. [Function # 15742]

  • The compiler needs to support C99  [Miscellaneous # 15347]  * specific information about the use of dialects please refer to: https://bugs.ruby-lang.org/projects/ruby-trunk/wiki/C99

See  NEWS  or  submit a log  to view details.

download link

  • https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.bz2

    SIZE: 14703381
    SHA1: b54f4633174dbc55db77d9fd6d0ef90cc35503af
    SHA256: 7aa247a19622a803bdd29fdb28108de9798abe841254fe8ea82c31d125c6ab26
    SHA512: 8b8dd0ceba65bdde53b7c59e6a84bc6bf634c676bfeb2ff0b3604c362c663b465397f31ff6c936441b3daabb78fb7a619be5569480c95f113dd0453488761ce7
  • https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.gz

    SIZE: 16799684
    SHA1: 6f4e99b5556010cb27e236873cb8c09eb8317cd5
    SHA256: 8c99aa93b5e2f1bc8437d1bbbefd27b13e7694025331f77245d0c068ef1f8cbe
    SHA512: 973fc29b7c19e96c5299817d00fbdd6176319468abfca61c12b5e177b0fb0d31174a5a5525985122a7a356091a709f41b332454094940362322d1f42b77c9927
  • https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.tar.xz

    SIZE: 11990900
    SHA1: 943c767cec037529b8e2d3cc14fc880cad5bad8d
    SHA256: 27d350a52a02b53034ca0794efe518667d558f152656c2baaf08f3d0c8b02343
    SHA512: dd5690c631bf3a2b76cdc06902bcd76a89713a045e136debab9b8a81ff8c433bbb254aa09e4014ca1cf85a69ff4bcb13de11da5e40c224e7268be43ef2194af7
  • https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.0.zip

    SIZE: 20571744
    SHA1: fbebdd3a2a641f9a81f7d8db5abd926acea27e80
    SHA256: 8bf2050fa1fc76882f878fd526e4184dc54bd402e385efa80ef5fd3b810522e0
    SHA512: 5060f2dd3bfd271ef255b17589d6d014260d7ec2d97b48112b717ee01c62fe125c3fe04f813e02d607cea3f0a2a812b14eb3a28d06c2551354dfeff5f4c3dd6b

Enjoy using Ruby 2.7 programming it!

Guess you like

Origin www.oschina.net/news/112338/ruby-2-7-0-released