mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
NEWS: structured the "Language changes" section
There were too many items in the section in somewhat random order. This change creates the following five subsections: * Pattern matching * The spec of keyword arguments is changed towards 3.0 * Numbered parameter * proc/lambda without no block is deprecated * Other miscellaneous changes Also it adds a handful of example code.
This commit is contained in:
parent
5d63a9da40
commit
c8f97d1620
1 changed files with 74 additions and 13 deletions
87
NEWS
87
NEWS
|
@ -14,10 +14,40 @@ sufficient information, see the ChangeLog file or Redmine
|
||||||
|
|
||||||
=== Language changes
|
=== Language changes
|
||||||
|
|
||||||
|
==== Pattern matching
|
||||||
|
|
||||||
* Pattern matching is introduced as an experimental feature. [Feature #14912]
|
* Pattern matching is introduced as an experimental feature. [Feature #14912]
|
||||||
|
|
||||||
* Method reference operator, <code>.:</code> is introduced as an
|
case [0, [1, 2, 3]]
|
||||||
experimental feature. [Feature #12125] [Feature #13581]
|
in [a, [b, *c]]
|
||||||
|
p a #=> 0
|
||||||
|
p b #=> 1
|
||||||
|
p c #=> [2, 3]
|
||||||
|
end
|
||||||
|
|
||||||
|
case {a: 0, b: 1}
|
||||||
|
in {a: 0, x: 1}
|
||||||
|
:unreachable
|
||||||
|
in {a: 0, b: var}
|
||||||
|
p var #=> 1
|
||||||
|
end
|
||||||
|
|
||||||
|
json = <<END
|
||||||
|
{
|
||||||
|
"name": "Alice",
|
||||||
|
"age": 30,
|
||||||
|
"children": [{ "name": "Bob", "age": 2 }]
|
||||||
|
}
|
||||||
|
END
|
||||||
|
if JSON.parse(json, symbolize_names: true) in
|
||||||
|
{name: "Alice", children: [{name: "Bob", age: age}]}
|
||||||
|
p age #=> 2
|
||||||
|
end
|
||||||
|
|
||||||
|
* See the following slides in detail
|
||||||
|
* https://speakerdeck.com/k_tsj/pattern-matching-new-feature-in-ruby-2-dot-7
|
||||||
|
|
||||||
|
==== The spec of keyword arguments is changed towards 3.0
|
||||||
|
|
||||||
* Automatic conversion of keyword arguments and positional arguments is
|
* Automatic conversion of keyword arguments and positional arguments is
|
||||||
deprecated, and conversion will be removed in Ruby 3. [Feature #14183]
|
deprecated, and conversion will be removed in Ruby 3. [Feature #14183]
|
||||||
|
@ -64,6 +94,9 @@ sufficient information, see the ChangeLog file or Redmine
|
||||||
* Non-symbols are allowed as a keyword argument keys if method accepts
|
* Non-symbols are allowed as a keyword argument keys if method accepts
|
||||||
arbitrary keywords. [Feature #14183]
|
arbitrary keywords. [Feature #14183]
|
||||||
|
|
||||||
|
* Non-Symbol keys in a keyword arguments hash were prohibited in 2.6.0,
|
||||||
|
but are now allowed again. [Bug #15658]
|
||||||
|
|
||||||
def foo(**kw); p kw; end; foo("str" => 1) #=> {"str"=>1}
|
def foo(**kw); p kw; end; foo("str" => 1) #=> {"str"=>1}
|
||||||
|
|
||||||
* <code>**nil</code> is allowed in method definitions to explicitly mark
|
* <code>**nil</code> is allowed in method definitions to explicitly mark
|
||||||
|
@ -86,17 +119,32 @@ sufficient information, see the ChangeLog file or Redmine
|
||||||
h = {}; def foo(*a) a end; foo(h) # [{}]
|
h = {}; def foo(*a) a end; foo(h) # [{}]
|
||||||
h = {}; def foo(a) a end; foo(h) # {}
|
h = {}; def foo(a) a end; foo(h) # {}
|
||||||
|
|
||||||
* Proc.new and Kernel#proc with no block in a method called with a block is
|
==== Numbered parameter
|
||||||
warned now.
|
|
||||||
|
|
||||||
* Kernel#lambda with no block in a method called with a block errs.
|
|
||||||
|
|
||||||
* Non-Symbol keys in a keyword arguments hash were prohibited in 2.6.0,
|
|
||||||
but are now allowed again. [Bug #15658]
|
|
||||||
|
|
||||||
* Numbered parameter as the default block parameter is introduced as an
|
* Numbered parameter as the default block parameter is introduced as an
|
||||||
experimental feature. [Feature #4475]
|
experimental feature. [Feature #4475]
|
||||||
|
|
||||||
|
[1, 2, 10].map { _1.to_s(16) } #=> ["1", "2", "a"]
|
||||||
|
|
||||||
|
==== proc/lambda without no block is deprecated
|
||||||
|
|
||||||
|
* Proc.new and Kernel#proc with no block in a method called with a block is
|
||||||
|
warned now.
|
||||||
|
|
||||||
|
def foo
|
||||||
|
proc
|
||||||
|
end
|
||||||
|
foo { puts "Hello" } #=> warning: Capturing the given block using Proc.new is deprecated; use `&block` instead
|
||||||
|
|
||||||
|
* Kernel#lambda with no block in a method called with a block raises an exception.
|
||||||
|
|
||||||
|
def bar
|
||||||
|
lambda
|
||||||
|
end
|
||||||
|
bar { puts "Hello" } #=> tried to create Proc object without a block (ArgumentError)
|
||||||
|
|
||||||
|
==== Other miscellaneous changes
|
||||||
|
|
||||||
* A beginless range is experimentally introduced. It might not be as useful
|
* A beginless range is experimentally introduced. It might not be as useful
|
||||||
as an endless range, but would be good for DSL purpose. [Feature #14799]
|
as an endless range, but would be good for DSL purpose. [Feature #14799]
|
||||||
|
|
||||||
|
@ -112,10 +160,9 @@ sufficient information, see the ChangeLog file or Redmine
|
||||||
* Quoted here-document identifier must end within the same line.
|
* Quoted here-document identifier must end within the same line.
|
||||||
|
|
||||||
<<"EOS
|
<<"EOS
|
||||||
" # This has been warned since 2.4
|
" # This had been warned since 2.4; Now it raises a SyntaxError
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
|
|
||||||
* The flip-flop syntax deprecation is reverted. [Feature #5400]
|
* The flip-flop syntax deprecation is reverted. [Feature #5400]
|
||||||
|
|
||||||
* Comment lines can be placed between fluent dot now.
|
* Comment lines can be placed between fluent dot now.
|
||||||
|
@ -134,6 +181,9 @@ sufficient information, see the ChangeLog file or Redmine
|
||||||
# Previously parsed as: (a, b = raise) rescue [1, 2]
|
# Previously parsed as: (a, b = raise) rescue [1, 2]
|
||||||
# Now parsed as: a, b = (raise rescue [1, 2])
|
# Now parsed as: a, b = (raise rescue [1, 2])
|
||||||
|
|
||||||
|
* Method reference operator, <code>.:</code> is introduced as an
|
||||||
|
experimental feature. [Feature #12125] [Feature #13581]
|
||||||
|
|
||||||
=== Core classes updates (outstanding ones only)
|
=== Core classes updates (outstanding ones only)
|
||||||
|
|
||||||
Array::
|
Array::
|
||||||
|
@ -148,6 +198,10 @@ Comparable::
|
||||||
|
|
||||||
* Comparable#clamp now accepts a Range argument. [Feature #14784]
|
* Comparable#clamp now accepts a Range argument. [Feature #14784]
|
||||||
|
|
||||||
|
-1.clamp(0..2) #=> 0
|
||||||
|
1.clamp(0..2) #=> 1
|
||||||
|
3.clamp(0..2) #=> 2
|
||||||
|
|
||||||
Complex::
|
Complex::
|
||||||
|
|
||||||
New method::
|
New method::
|
||||||
|
@ -163,7 +217,9 @@ Dir::
|
||||||
|
|
||||||
Encoding::
|
Encoding::
|
||||||
|
|
||||||
* Added new encoding CESU-8 [Feature #15931]
|
New method::
|
||||||
|
|
||||||
|
* Added new encoding CESU-8 [Feature #15931]
|
||||||
|
|
||||||
Enumerable::
|
Enumerable::
|
||||||
|
|
||||||
|
@ -171,8 +227,12 @@ Enumerable::
|
||||||
|
|
||||||
* Added Enumerable#filter_map. [Feature #15323]
|
* Added Enumerable#filter_map. [Feature #15323]
|
||||||
|
|
||||||
|
[1, 2, 3].filter_map {|x| x.odd? ? x.to_s : nil } #=> ["1", "3"]
|
||||||
|
|
||||||
* Added Enumerable#tally. [Feature #11076]
|
* Added Enumerable#tally. [Feature #11076]
|
||||||
|
|
||||||
|
["A", "B", "C", "B", "A"].tally #=> {"A"=>2, "B"=>2, "C"=>1}
|
||||||
|
|
||||||
Enumerator::
|
Enumerator::
|
||||||
|
|
||||||
New method::
|
New method::
|
||||||
|
@ -192,6 +252,7 @@ Fiber::
|
||||||
exception on the resumed fiber. [Feature #10344]
|
exception on the resumed fiber. [Feature #10344]
|
||||||
|
|
||||||
File::
|
File::
|
||||||
|
|
||||||
Modified method::
|
Modified method::
|
||||||
|
|
||||||
* File.extname now returns a dot string at a name ending with a dot on
|
* File.extname now returns a dot string at a name ending with a dot on
|
||||||
|
@ -311,7 +372,7 @@ Time::
|
||||||
|
|
||||||
UnboundMethod::
|
UnboundMethod::
|
||||||
|
|
||||||
New methods::
|
New method::
|
||||||
|
|
||||||
* Added UnboundMethod#bind_call method. [Feature #15955]
|
* Added UnboundMethod#bind_call method. [Feature #15955]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue