Last active
August 29, 2015 14:15
-
-
Save thash/665ffe025267c8c10a1e to your computer and use it in GitHub Desktop.
This file contains 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
RUBY_VERSION #=> "2.1.2" | |
def foo; 123 end | |
def bar(foo = foo) | |
p foo # => 123 | |
p foo.class # => Fixnum | |
end | |
# Ruby2.2からは警告を出した上で、nilが代入されるようになる | |
RUBY_VERSION #=> "2.2.0" | |
def foo; 123 end | |
def bar(foo = foo) | |
p foo # => nil | |
p foo.class # => NilClass | |
end | |
# => warning: circular argument reference - foo |
This file contains 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
RUBY_VERSION #=> "2.2.0" | |
def foo; 123 end | |
# 以前と同じ挙動をさせたい場合はメソッド呼び出しであることを明確にするため()を付ける | |
def bar(foo = foo()) | |
p foo # => 123 | |
p foo.class # => Fixnum | |
end |
This file contains 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
# メソッド引数以外では以前からこの動作 | |
RUBY_VERSION #=> "2.1.2" | |
x #=> NameError: undefined local variable or method `x' for main:Object | |
x = x #=> nil | |
x #=> nil | |
RUBY_VERSION #=> "2.2.0" | |
x #=> NameError: undefined local variable or method `x' for main:Object | |
x = x #=> nil | |
x #=> nil |
This file contains 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
RUBY_VERSION #=> "2.1.2" | |
a = 1 | |
bnd = binding #=> #<Binding:0x007fdaab3f3788> | |
bnd.eval('local_variables') #=> [:bnd, :a, ...] | |
bnd.local_variables #=> NoMethodError: private method `local_variables' called for #<Binding:0x007fdaab3f3788> | |
RUBY_VERSION #=> "2.2.0" | |
a = 1 | |
bnd = binding #=> #<Binding:0x007f99ce089b88> | |
bnd.eval('local_variables') #=> [:bnd, :a, ...] | |
bnd.local_variables #=> [:bnd, :a, ...] |
This file contains 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
RUBY_VERSION #=> 2.2.0 | |
p File.open('local_variables.rb').birthtime | |
# => 2015-02-20 07:29:10 +0900 | |
p File.open('local_variables.rb').ctime | |
# File#mtimeは最終更新時間を返す | |
p File.open('local_variables.rb').mtime | |
# => 2015-02-21 01:26:17 +0900 |
This file contains 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
def mul(a, b) | |
a * b | |
end | |
# メソッドオブジェクトを取得 | |
m = method(:mul) #=> #<Method: Object#mul> | |
# Methodをcurry化するとProcになります | |
p prc = m.curry #=> #<Proc:0x007fe0fa08a6c8 (lambda)> | |
# カリー化されたprocに対して、引数の部分適用が可能になります | |
p mul10 = prc.call(10) #=> #<Proc:0x007fea15885e08 (lambda)> | |
# のこりひとつの引数を適用します | |
p mul10.call(21) #=> 210 | |
# ちなみに.call(xxx)は.(xxx)とも書けます | |
p mul10.(21) |
This file contains 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
p 2.0.next_float #=> 2.0000000000000004 | |
p 2.0.prev_float #=> 1.9999999999999998 |
This file contains 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
RUBY_VERSION # => 2.1.2 | |
[true, false, nil].map(&:frozen?) | |
=> [false, false, false] | |
RUBY_VERSION # => 2.2.0 | |
[true, false, nil].map(&:frozen?) | |
=> [true, true, true] |
This file contains 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
RUBY_VERSION #=> 2.1.2 | |
{ "super-key": 1, "hyper-key": 2 } | |
# => SyntaxError: unexpected ':', expecting => | |
# { "super-key": 1, "hyper-key": 2 } | |
# ^ | |
# こう書く必要があった | |
{ :"super-key" => 1, :"hyper-key" => 2 } | |
# => {:"super-key"=>1, :"hyper-key"=>2} | |
RUBY_VERSION #=> 2.2.0 | |
{ "super-key": 1, "hyper-key": 2 } | |
#=> {:"super-key"=>1, :"hyper-key"=>2} |
This file contains 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
ary = [2, 3, 0, 3, 4, 2, 4, 3, 0, 2] | |
ary.group_by(&:itself) | |
#=> {2=>[2, 2, 2], 3=>[3, 3, 3], 0=>[0, 0], 4=>[4, 4]} | |
# 2.1以前はitselfがないのでこう書くしかない | |
ary.group_by{|i| i } | |
#=> {2=>[2, 2, 2], 3=>[3, 3, 3], 0=>[0, 0], 4=>[4, 4]} |
This file contains 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
RUBY_VERSION #=> 2.1.2 | |
lam = ->(word, index) { word.length == 3 } | |
#=> #<Proc:0x007fe112b26ba0@(pry):27 (lambda)> | |
%w(Hi there how are you).each_with_index.detect &lam | |
#=> ArgumentError: wrong number of arguments (1 for 2) | |
RUBY_VERSION #=> 2.2.0 | |
lam = ->(word, index) { word.length == 3 } | |
#=> #<Proc:0x007f856e44cb90@(pry):70 (lambda)> | |
%w(Hi there how are you).each_with_index.detect &lam | |
#=> ["how", 2] |
This file contains 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
a = 1 | |
b = 2 | |
RUBY_VERSION # => 2.1.2 | |
local_variables # => [:a, :b] | |
binding.eval('local_variables') # => [:a, :b] | |
binding.local_variables | |
# => NoMethodError: private method `local_variables' called for #<Binding:0x007fe1133b8548> | |
RUBY_VERSION # => 2.2.0 | |
local_variables # => [:a, :b] | |
binding.eval('local_variables') # => [:a, :b] | |
binding.local_variables # => [:a, :b] |
This file contains 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
RUBY_VERSION #=> "2.2.0" | |
p Math.log(2.7) #=> 0.9932517730102834 | |
p Math.log(2.8) #=> 1.0296194171811581 | |
p Math.log(10, 2) #=> 3.3219280948873626 | |
p Math.log(10, -2) | |
#=> Numerical argument is out of domain - "log" (Math::DomainError) |
This file contains 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
RUBY_VERSION #=> 2.1.2 | |
Math.log(Math::E, -2) #=> NaN | |
RUBY_VERSION #=> 2.2.0 | |
Math.log(Math::E, -2) | |
#=> Math::DomainError: Numerical argument is out of domain - "log" |
This file contains 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
[2, 5, 3, 4, 1].max #=> 5 | |
[2, 5, 3, 4, 1].max(3) #=> [5, 4, 3] |
This file contains 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
RUBY_VERSION # => 2.1.2 | |
binding.eval('self') # => main | |
binding.receiver | |
#=> NoMethodError: undefined method `receiver' for #<Binding:0x007fe1159032f8> | |
RUBY_VERSION # => 2.2.0 | |
binding.eval('self') # => main | |
binding.receiver # => main |
This file contains 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
p [1, 3, :a, 4, 5, 6].slice_before{|e| e.is_a? Symbol}.to_a | |
#=> [[1, 3], [:a, 4, 5, 6]] | |
p [1, 3, :a, 4, 5, 6].slice_after{|e| e.is_a? Symbol}.to_a | |
#=> [[1, 3, :a], [4, 5, 6]] | |
# ブロックの中身がis_aならクラスを受け取って次のように書いても同じ | |
[1, 3, :a, 4, 5, 6].slice_after(Symbol).to_a | |
# slice_when | |
p [1, 3, 4, 5, 6, 8, 10, 12].slice_when{|e| e.odd? }.to_a | |
#=> [[1], [3], [4, 5], [6, 8, 10, 12]] | |
# 比較もできる. 値が2以上ジャンプしたところでsliceする例 | |
p [1, 3, 4, 5, 6, 8, 10, 12].slice_when{|e1, e2| e1 + 1 < e2 }.to_a | |
#=> [[1], [3, 4, 5, 6], [8], [10], [12]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment