Mode:
Duration:
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
class Calculator
def initialize
@history = []
end
def add(a, b)
result = a + b
@history << "#{a} + #{b} = #{result}"
result
end
def multiply(a, b)
result = a * b
@history << "#{a} * #{b} = #{result}"
result
end
def history
@history.each_with_index do |operation, index|
puts "#{index + 1}. #{operation}"
end
end
end
# Array operations with blocks
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = numbers.select { |n| n.even? }
squares = numbers.map { |n| n ** 2 }
sum = numbers.reduce(0) { |acc, n| acc + n }
puts "Even numbers: #{even_numbers}"
puts "Squares: #{squares}"
puts "Sum: #{sum}"
# Calculator usage
calc = Calculator.new
calc.add(5, 3)
calc.multiply(4, 6)
calc.historyCoding works best on desktop or with an external keyboard.