Created
November 23, 2012 11:32
-
-
Save aflc/4135223 to your computer and use it in GitHub Desktop.
Ruby1.9のブロックについて勉強した ref: http://qiita.com/items/413f710b126a869cb797
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
(1..3).map {|x| x * 2} # => [2, 4, 6] |
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
a = (1..3).to_enum | |
a.next() # => 1 |
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
def f1(x) | |
x * 2 | |
end | |
(1..3).map {|x| f1(x)} # => [2, 4, 6] |
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
f1 = lambda {|x| x * 2} # f1とf2は同じ意味。f1とf3はスコープの考えがちょっと違う(割愛) | |
f2 = ->(x) {x * 2} | |
f3 = Proc.new {|x| x * 2} | |
(1..3).map &f1 # => [2, 4, 6] | |
(1..3).map &f2 # => [2, 4, 6] | |
(1..3).map &f3 # => [2, 4, 6] |
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
def itr_func | |
(1..3).each {|x| yield x} | |
end | |
l = [] | |
itr_func {|x| l << x} | |
l # => [1, 2, 3] |
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
enum = Enumerator.new do |yielder| | |
yielder.yield 1 | |
yielder.yield 2 | |
yielder.yield 3 | |
end | |
enum.next() # => 1 | |
enum.next() # => 2 | |
enum.next() # => 3 |
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
def itr_func | |
(1..3).each {|x| yield x} | |
end | |
enum = Enumerator.new do |yielder| | |
Object.itr_func {|x| yielder.yield x} | |
end | |
enum.next() # => 1 | |
enum.next() # => 2 |
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
enum = ->(x){x * 2} | |
(1..3).map(&enum).map(&enum) # => [4, 8, 12] |
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
class Chainer | |
attr_reader :enum | |
def initialize(enum) | |
@enum = enum | |
end | |
def chain(&block) | |
next_enum = Enumerator.new do |yielder| | |
loop do | |
yielder.yield block.call(enum.next) | |
end | |
end | |
Chainer.new(next_enum) | |
end | |
end | |
enum = Enumerator.new do |yielder| | |
yielder.yield 1 | |
yielder.yield 2 | |
yielder.yield 3 | |
end | |
proder = ->(x) {x * 2} | |
Chainer.new(enum).chain(&proder).chain(&proder).enum.to_a # => [4, 8, 12] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment