generated from javitocor/Ruby-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.txt
113 lines (91 loc) · 3.67 KB
/
examples.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Most of those examples comes from https://apidock.com/ruby/
# Copy all the examples and paste into enumerable.rb, run the code to check that all methods works correctly
puts 'my_each'
[1, 2, 3, 4, 'hi'].my_each do |x|
puts x
end
puts [2,4,7,11].my_each #<Enumerator: [2, 4, 7, 11]:my_each>
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_each_with_index'
[1, 2, 3, 4, 'hi'].my_each_with_index { |value, index| puts "#{value} => #{index}" }
puts [2,4,7,11].my_each_with_index #<Enumerator: [2, 4, 7, 11]:my_each_with_index
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_select'
result = [1, 2, 3, 4, 5, 6].select(&:even?) #=> [2, 4, 6]
puts result
block = proc { |num| num = 11 }
puts [2,4,7,11].my_select(&block) #=> [11]
puts [2,4,7,11].my_select #<Enumerator: [2, 4, 7, 11]:my_select>
puts [1, 2].my_select { |num| num == 1 } #=> [1]
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_all?'
puts %w[ant bear cat].my_all? { |word| word.length >= 3 } #=> true
puts %w[ant bear cat].my_all? { |word| word.length >= 4 } #=> false
puts %w[ant bear cat].my_all?(/t/) #=> false
puts [1, 2i, 3.14].my_all?(Numeric) #=> true
puts [nil, true, 99].my_all? #=> false
puts [].my_all? #=> true
puts [nil, false, true, []].my_all? #=> false
puts [1, 2.5, 'a', 9].my_all?(Integer) #=> false
puts %w[dog door rod blade].my_all?(/d/) #=> true
puts [3,4,7,11].my_all?(3) #=> false
puts [1, false].my_all? #=> false
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_any??'
puts %w[ant bear cat].my_any? { |word| word.length >= 3 } #=> true
puts %w[ant bear cat].my_any? { |word| word.length >= 4 } #=> true
puts %w[ant bear cat].my_any?(/d/) #=> false
puts [nil, true, 99].my_any?(Integer) #=> true
puts [nil, true, 99].my_any? #=> true
puts [].my_any? #=> false
puts [nil, false, true, []].my_any? #=> true
puts [1, 2.5, 'a', 9].my_any?(Integer) #=> true
puts %w[dog door rod blade].my_any?(/d/) #=> true
puts [3,4,7,11].my_any?(3) #=> true
puts [1, false].my_any? #=> true
puts [1].my_any? #=> true
puts [nil].my_any? #=> false
puts [nil, false, nil, false].my_any? #=> false
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_none?'
puts %w[ant bear cat].my_none? { |word| word.length == 5 } #=> true
puts %w[ant bear cat].my_none? { |word| word.length >= 4 } #=> false
puts %w[ant bear cat].my_none?(/d/) #=> true
puts [1, 3.14, 42].my_none?(Float) #=> false
puts [].my_none? #=> true
puts [nil].my_none? #=> true
puts [nil, false].my_none? #=> true
puts [nil, false, true].my_none? #=> false
puts [nil, false, true, []].my_none? #=> false
puts [1, 2.5, 'a', 9].my_none?(Integer) #=> false
puts %w[dog door rod blade].my_none?(/d/) #=> false
puts [3,4,7,11].my_none?(3) #=> false
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_count'
ary = [1, 2, 4, 2]
puts ary.my_count #=> 4
puts ary.my_count(2) #=> 2
puts ary.my_count(&:even?) #=> 3
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_map'
puts (1..4).my_map { |i| i * i } #=> [1, 4, 9, 16]
puts (1..4).my_map { 'dog' } #=> ["dog", "dog", "dog", "dog"]
puts %w[a b c].my_map(&:upcase) #=> ["A", "B", "C"]
puts %w[a b c].my_map(&:class) #=> [String, String, String]
puts [2,4,7,11].my_map #<Enumerator: [2, 4, 7, 11]:my_map
my_proc = Proc.new {|num| num > 10 }
puts [18, 22, 5, 6] .my_map(my_proc) {|num| num < 10 } # true true false false
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'my_inject'
longest = %w[cat sheep bear].my_inject do |memo, word|
memo.length > word.length ? memo : word
end
puts longest #=> "sheep"
puts (5..10).my_inject { |sum, n| sum + n } #=> 45
puts (5..10).my_inject(2) { |sum, n| sum + n } #=> 47
puts (1..5).my_inject(4) { |prod, n| prod * n } #=> 480
puts [1, 1, 1].my_inject(:+) #=> 3
puts [1, 1, 1].my_inject(2, :+) #=> 5
puts '-*-*-*-*-*-*-*-*-*-*-*-*-'
puts 'multiply_els'
puts multiply_els([2, 4, 5]) #=> 40