-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheatsheet.html
More file actions
106 lines (104 loc) · 2.72 KB
/
cheatsheet.html
File metadata and controls
106 lines (104 loc) · 2.72 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Ruby Enumerable Cheatsheet</title>
<link rel="stylesheet" type="text/css" href="CheatSheetStyleSheet.css">
</head>
<body>
<header>
<h2>Ruby Enumerable Cheatsheet</h2>
</header>
<ul>
<li>
<h3>>> Enumerable.instance_methods.sort</h3>
=> [:all?, :any?, :chunk, :collect, :collect_concat, :count, :cycle,
:detect, :drop, :drop_while, :each_cons, :each_entry, :each_slice,
:each_with_index, :each_with_object, :entries, :find, :find_all,
:find_index, :first, :flat_map, :grep, :group_by, :include?, :inject,
:map, :max, :max_by, :member?, :min, :min_by, :minmax, :minmax_by,
:none?, :one?, :partition, :reduce, :reject, :reverse_each, :select,
:slice_before, :sort, :sort_by, :take, :take_while, :to_a, :zip]
<br></br>
</li>
<li>
<h3>Searching:</h3>
>> [1,2,3,4,5].select { |i| i > 3 }
<br></br>
=> [4,5]
<br></br>
>> [1,2,3,4,5].detect { |i| i > 3 }
<br></br>
=> 4
<br></br>
>> [1,2,3,4,5].reject { |i| i > 3 }
<br></br>
=> [1,2,3]
<br></br>
</li>
<li>
<h3>Sorting:</h3>
>> [1, "5", 2, "7", "3"].sort_by { |a| a.to_i }
<br></br>
=> [1, 2, "3", "5", "7"]
<br></br>
>> [1, "5", 2, "7", "3"].sort_by(&:to_i)
<br></br>
=> [1, 2, "3", "5", "7"]
</li>
<h3>Any? and All?:</h3>
>> [2,4,6,8].all?(&:even?)
<br></br>
=> true
<br></br>
>> [2,4,5,8].any? { |i| i % 2 == 0 }
<br></br>
=> true
<br></br>
>> [2,4,5,8].all? { |i| i % 2 == 0 }
<br></br>
=> false
<br></br>
<li>
<h3>Reduce:</h3>
>> [1,2,3].reduce(:+)
<br></br>
=> 6
<br></br>
</li>
<li>
<h3>Each:</h3>
>> hash = Hash.new
<br></br>
%w(cat dog wombat).each_with_index { |item, index|
<br></br>
hash[item] = index
}
hash #=> {"cat"=>0, "dog"=>1, "wombat"=>2}
</li>
<li>
<h3>Include?:</h3>
IO.constants.include? :SEEK_SET #=> true
<br></br>
IO.constants.include? :SEEK_NO_FURTHER #=> false
<br></br>
</li>
<li>
<h3>MAP:</h3>
(1..4).map { |i| i*i } #=> [1, 4, 9, 16]
<br></br>
(1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"]
<br></br>
</li>
<li>
<h3>Reverse:</h3>
(1..3).reverse_each { |v| p v }
<br></br>
produces:
<br></br>
3
2
1
</li>
</ul>
</body>
</html>