- @hamajyotan (SAKAGUCHI Takashi)
- office ummm
- rubyist
- slowlife engineer (farmer, programmer)
https://github.com/ruby/ruby/blob/ruby_2_3/NEWS
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
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
(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]
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"]
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
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 <= h2 #=> true
h2 <= h1 #=> false
h1 <= h1 #=> true
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 < h2 #=> true
h2 < h1 #=> false
h1 < h1 #=> false
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 >= h2 #=> false
h2 >= h1 #=> true
h1 >= h1 #=> true
h1 = {a:1, b:2}
h2 = {a:1, b:2, c:3}
h1 > h2 #=> false
h2 > h1 #=> true
h1 > h1 #=> false
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]
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]
> "hoge".to_b
NoMethodError: undefined method `to_b' for "hoge":String
Did you mean? to_r
to_f
to_i
to_s
to_c
$ 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)
$ 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
****************************************
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
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]]
x = "string"
begin
x.unknown_method!
rescue NameError => ex
puts x.equal?(ex.receiver) #=> true
end
"hoge".freeze.frozen? #=> true
+"hoge".frozen? #=> false
-"hoge".frozen? #=> true
class Foo
def to_ary
[1, 2, 3]
end
end
[:hoge, Foo.new, :fuga].flatten #=> [:hoge, 1, 2, 3, :fuga]
- changelog
- reference manual