generated from zmekonnen251/MorseCode-decoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathour_list.rb
53 lines (40 loc) · 791 Bytes
/
our_list.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
require_relative 'our_enumerable'
class List
include Enumerable
def initialize(*items)
@items = items
end
def each
@items.length.times { |i| yield @items[i] }
end
end
# Create our list
my_list = List.new(1, 4, 6, 9, -2, 14, 3, 4)
puts(my_list)
# <MyList: @list=[1, 4, 6,9,-2,14, 3, 4]>
# Test #all?
puts(my_list.all? { |e| e < 5 })
# => false
puts(my_list.all? { |e| e > 5 })
# => false
puts(my_list.all? { |e| e > -3 })
# => true
# Test #any?
puts(my_list.any? { |e| e == -2 })
# => true
puts(my_list.any? { |e| e == 5 })
# => false
# Test #max
puts(my_list.max)
# => 14
# Test #min
puts(my_list.min)
# => -2
# Test #sort
print(my_list.sort)
# => [-2, 1, 3, 4, 4, 6, 9, 14]
puts ''
# Test #filter
print(my_list.filter(&:even?))
puts ''
# => [4, 6, -2, 14, 4]