-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.rb
55 lines (39 loc) · 788 Bytes
/
blocks.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
# frozen_string_literal: false
transactions = [10, -15, 25, 30, -24, -70, 999]
def transaction_statement(transactions)
formatted_transactions = []
transactions.each do |transaction|
formatted_transactions << yield(transaction)
end
p formatted_transactions
end
transaction_statement(transactions) do |transaction|
'%0.2f' % transaction
end
my_name = ->(name) { puts "hello #{name}" }
my_name.call('tim')
my_name.('tim')
my_name['tim']
my_name.=== 'tim'
class Timmy
attr_reader :value
def initialize(value)
@value = value
end
def double_me
@value *= 2
end
def to_i
@value
end
end
arr = [Timmy.new(1), Timmy.new(2), Timmy.new(3)]
p arr.map(&:double_me)
def run_block
yield
puts 'World'
end
run_block do
puts 'Hello'
return
end