Last active
May 22, 2016 21:52
-
-
Save esehara/57cf43ae03d33a4a0a95e72ae12996b5 to your computer and use it in GitHub Desktop.
あとで処理したいと思ったらProcに相談だ!! ref: http://qiita.com/esehara@github/items/0e2d5fee050278ff74e6
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
class Env | |
def initialize | |
@values = {} | |
end | |
def plus(x, y) | |
return x + y | |
end | |
def get_value(x) | |
@values[x] | |
end | |
def set_value(x, y) | |
@values[x] = y | |
end | |
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
env = Env.new | |
env.set_value("x", 3) | |
puts env.plus( | |
env.plus( | |
env.get_value("x"), | |
env.set_value("x", 6)), | |
env.get_value("x")) |
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
class Env | |
def initialize | |
@values = {} | |
end | |
def plus(x, y) | |
-> {x[] + y[]} | |
end | |
def get_value(x) | |
-> { @values[x] } | |
end | |
def set_value(x, y) | |
@values[x] = y | |
-> {y} | |
end | |
end | |
env = Env.new | |
env.set_value("x", 3) | |
puts env.plus( | |
env.get_value("x"), | |
env.plus( | |
env.set_value("x", 6), | |
env.get_value("x") | |
))[] |
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
env = Env.new | |
env.set_value("x", 3) | |
puts env.plus( | |
env.plus( # 4. => 3と6を受けとったので、9を返す | |
env.get_value("x"), # => 1. xを参照し、3を返す | |
env.set_value("x", 6)), # => 2 6をxにセットし、6を返す | |
env.get_value("x")) # => 3 xを参照し、6を返す | |
# 5. => 15が返る |
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
env = Env.new | |
env.set_value("x", 3) | |
puts env.plus( | |
env.get_value("x"), # 4. さらにProcを返す | |
env.plus( # 3. 引数が揃ったので、Procを実行するProcを返す | |
env.set_value("x", 6), # => 1. まずxに6の値をセットするコードが走る | |
env.get_value("x") # => 2. Procを返す | |
))[] # 5. Procを実行する -> あとは順次実行 | |
# 6. => 18が返る |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment