Last active
November 12, 2021 02:33
-
-
Save jb08/865c732b511ea35a2e121a0e606d2e58 to your computer and use it in GitHub Desktop.
new Ruby 3 language features
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## "Pattern matching (for use in case/switch statements) | |
## is a feature allowing deep matching of structured values" | |
# version 2.6.5 | |
> {chicago: :cubs, stlouis: :cardinals, denver: :rockies} => {stlouis:} #=> SyntaxError | |
> 1 in 1 #=> SyntaxError | |
> 0 in 1 #=> SyntaxError | |
# version 3.0.1 | |
> {chicago: :cubs, stlouis: :cardinals, denver: :rockies} => {stlouis:} | |
> stlouis #=> :cardinals | |
> 1 in 1 #=> true | |
> 0 in 1 #=> false | |
## Hash#except (this Hash method was monkey-patched in Rails, now its built into Ruby) | |
# version 2.6.5 | |
> {foo: 3, bar: 4, baz: 5}.except(:bar) #=> NoMethodError | |
# version 3.0.1 | |
> {foo: 3, bar: 4, baz: 5}.except(:bar) #=> {:foo=>3, :baz=>5} | |
## "Endless" method definition | |
# version 2.6.5 | |
> def power(base, exponent) = base ** exponent #=> SyntaxError | |
# version 3.0.1 | |
> def power(base, exponent) = base ** exponent #=> :power | |
> power(2,4) #=> 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment