-
Notifications
You must be signed in to change notification settings - Fork 0
/
yield_self.rb
71 lines (57 loc) · 1.41 KB
/
yield_self.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'csv'
class Foo
def initialize(something)
@something = something
end
def double
@something * 2
end
end
puts 'call one method, store result in var'
puts 'a = Foo.new(2)'
a = Foo.new(2)
puts 'b = a.double'
b = a.double
puts 'c = b.to_f'
c = b.to_f
puts "Result: #{c}"
puts "\n-------------------------\n\n"
puts 'call chaining methods'
puts 'c = Foo.new(2).double.to_f'
c = Foo.new(2).double.to_f
puts "Result: #{c}"
puts "\n-------------------------\n\n"
puts 'yield_self with block'
puts 'c = id'
puts '.yield_self { |foo| foo.double}'
puts '.yield_self { |doubled| doubled.to_f}'
c = Foo.new(2)
.yield_self { |foo| foo.double}
.yield_self { |doubled| doubled.to_f}
puts "Result: #{c}"
puts "\n-------------------------\n\n"
puts 'yield_self with method'
puts 'Foo.new(2)'
puts '.yield_self(&:double)'
puts '.yield_self(&:to_f)'
c = Foo.new(2)
.yield_self(&:double)
.yield_self(&:to_f)
puts "Result: #{c}"
puts "\n-------------------------\n\n"
puts "what is more readable?"
path = File.expand_path("data.csv")
file = File.read(path)
csv = CSV.parse(file)
csv
.map { |row| row[1].to_i }
.sum
CSV.parse(File.read(File.expand_path("data.csv")))
.map { |row| row[1].to_i }
.sum
"data.csv"
.yield_self { |name| File.expand_path(name) }
.yield_self { |path| File.read(path) }
.yield_self { |body| CSV.parse(body) }
.map { |row| row[1].to_i }
.sum