Skip to content

Instantly share code, notes, and snippets.

@hamajyotan
Created February 13, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamajyotan/e6f684a4dc58164d7f95 to your computer and use it in GitHub Desktop.
Save hamajyotan/e6f684a4dc58164d7f95 to your computer and use it in GitHub Desktop.
第21回鳥取Ruby会とっとるびー勉強会資料

Ruby 2.3 変更点とか

p self

  • @hamajyotan (SAKAGUCHI Takashi)
  • office ummm
  • rubyist
  • slowlife engineer (farmer, programmer)

changelog

https://github.com/ruby/ruby/blob/ruby_2_3/NEWS

Array#dig

a = [ [1, [2, 3] ] ]

a.dig(0, 1, 1)                    #=> 3
a.dig(1, 2, 3)                    #=> nil
a.dig(0, 0, 0)                    #=> NoMethodError, undefined method `dig' for 1:Fixnum
[42, {foo: :bar}].dig(1, :foo)    #=> :bar

Hash#dig

h = { foo: {bar: {baz: 1}}}

h.dig(:foo, :bar, :baz)           #=> 1
h.dig(:foo, :zot, :xyz)           #=> nil

g = { foo: [10, 11, 12] }
g.dig(:foo, 1)                    #=> 11

Enumerable#grep_v

(1..10).grep_v 2..5                 #=> [1, 6, 7, 8, 9, 10]
(1..10).grep_v(2..5) { |v| v * 2 }  #=> [2, 12, 14, 16, 18, 20]

Hash#fetch_values

h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }

h.fetch_values("cow", "cat")                   #=> ["bovine", "feline"]
h.fetch_values("cow", "bird")                  # raises KeyError
h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]

Numeric#positive? Numeric#negative?

3.positive?     # => true
-2.positive?    # => false
3.0.positive?   # => true

3.negative?     # => false
-2.negative?    # => true
3.0.negative?   # => false

0.positive?     # => false
0.negative?     # => false
0.0.positive?   # => false
0.0.negative?   # => false

Hash#<=

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 <= h2   #=> true
h2 <= h1   #=> false
h1 <= h1   #=> true

Hash#<

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 < h2    #=> true
h2 < h1    #=> false
h1 < h1    #=> false

Hash#>=

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 >= h2   #=> false
h2 >= h1   #=> true
h1 >= h1   #=> true

Hash#>

h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 > h2    #=> false
h2 > h1    #=> true
h1 > h1    #=> false

Hash#to_proc

hash = { one: 1, two: 2, three: 3 }

proc = hash.to_proc
proc.call(:one)     #=> 1
proc.call(:four)    #=> nil

array = [:one, :three, :five]
array.map(&hash)          #=> [1, 3, nil]
array.map { |a| hash[a] } #=> [1, 3, nil]

Lonely operator

array = [1, 2, nil, 4]
array.map { |_| _.succ }  #=> raises NoMethodError
array.map { |_| _&.succ } #=> [2, 3, nil, 5]
array.map { |_| _&.to_s } #=> ["1", "2", nil, "5"]

io = $stdout
x = [1, 2, nil, 3]
io&.puts(x.compact!)
x                         #=> [1, 2, 4]

io = nil
x = [1, 2, nil, 3]
io&.puts(x.compact!)
x                         #=> [1, 2, nil, 4]

did_you_mean

> "hoge".to_b
NoMethodError: undefined method `to_b' for "hoge":String
Did you mean?  to_r
               to_f
               to_i
               to_s
               to_c

Frozen String Literal Pragma

$ cat frozen.rb
# frozen-string-literal: true

x = "I love"
x << " ruby"
puts x

$ ruby frozon.rb
frozen.rb:4:in `<main>': can't modify frozen String (RuntimeError)

indented here document

$ cat ident.rb
def foo
  <<~EOS
  hoge
  fuga
  piyo
  EOS
end

def bar
  <<-EOS
  hoge
  fuga
  piyo
  EOS
end

puts "*"*40
puts foo
puts "*"*40
puts bar
puts "*"*40

$ ruby ident.rb
****************************************
hoge
fuga
piyo
****************************************
  hoge
  fuga
  piyo
****************************************

Array#bsearch_index

ary = [0, 4, 7, 10, 12]
ary.bsearch { |x| x >=   4 } #=> 4
ary.bsearch { |x| x >=   6 } #=> 7
ary.bsearch { |x| x >=  -1 } #=> 0
ary.bsearch { |x| x >= 100 } #=> nil
ary.bsearch_index { |x| x >=   4 } #=> 1
ary.bsearch_index { |x| x >=   6 } #=> 2
ary.bsearch_index { |x| x >=  -1 } #=> 0
ary.bsearch_index { |x| x >= 100 } #=> nil

Array#chunk_while

a = [1,2,4,9,10,11,12,15,16,19,20,21]
b = a.chunk_while {|i, j| i+1 == j }
b.to_a #=>  [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]

a = [0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
a.chunk_while {|i, j| i <= j }.to_a #=> [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]

NameError#receiver

x = "string"
begin
  x.unknown_method!
rescue NameError => ex
  puts x.equal?(ex.receiver)  #=> true
end

String#+@, String#-@

"hoge".freeze.frozen? #=> true
+"hoge".frozen? #=> false
-"hoge".frozen? #=> true

Array#flatten, Array#flatten!

class Foo
  def to_ary
    [1, 2, 3]
  end
end
[:hoge, Foo.new, :fuga].flatten #=> [:hoge, 1, 2, 3, :fuga]

reference documents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment