-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenumerable-methods.html
129 lines (111 loc) · 6.99 KB
/
enumerable-methods.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="../stylesheets/blog.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<title>Bill's Technical Blog: Ruby#Enumerable#Map</title>
</head>
<body>
<div class="outer-div">
<div class="inner-left">
<aside id="sidebar-fixed">
<ul class="buttons-ul">
<li><a href="index.html"><i class="fa fa-bars"></i><br>Blog Page</a></li>
<li><a href="../index.html"><i class="fa fa-home"></i><br>Home Page</a></li>
<li><a href="https://www.facebook.com/" target="_blank"><i class="fa fa-facebook-official"></i><br>Facebook</a></li>
<li><a href="https://twitter.com/" target="_blank"><i class="fa fa-twitter-square"></i><br>Twitter</a></li>
</ul>
</aside>
</div>
<div class="inner-right">
<main>
<h1>Ruby Method: Map</h1>
<p>
March 10, 2016
</p>
<div class="blog-img-div">
<img src="../imgs/ruby-collect-map.jpg" alt="Ruby Map Method" width="500" height="400">
</div>
<section>
<h2>The Map Method</h2>
<p>
The map method is a highly useful Ruby method. Let's look at a real life example,
you own a store and you have a list of products with their prices. One day you
decided to double the price of all your products...because it's your store and
you just felt like it. This is where we use the map method.
</p>
<p>
So what does the map method do? Map goes through a provided object, does something
to it then gives you the results. Some examples of what this looks like:
</p>
<pre class="ruby-code">
my_array = [2, 4, 6, 8, 100]
my_array.map { |num| num + 2 }
=> [4, 6, 8, 10, 102]
</pre>
<ul>
<li>In this example, <code class="ruby-code">my_array</code> is an array (aka an object)
and it contains 5 numbers: <code class="ruby-code">[2,4,6,8,100]</code></li>
<li>We use the map method on it by adding a period after <code class="ruby-code">my_array</code>
followed by <code class="ruby-code">map</code></li>
<li>Then we add a bracket and a vertical bar, both of which are part of the map method's syntax</li>
<li>Next we see <code class="ruby-code">num</code> which is just a name I made up as a variable to
represent each of the 5 numbers in <code class="ruby-code">my_array</code> but you can use something
else like <code class="ruby-code">x</code></li>
<li>After that we see a closing veritical bar</li>
<li>Finally we see <code class="ruby-code">num + 2</code> which means we are using each of the 5 numbers in
<code class="ruby-code">my_array</code> and adding 2 to them and of course we have the closing bracket</li>
<li>If we use the above codes, we'll see an output of <code class="ruby-code">[4, 6, 8, 10, 102]</code></li>
</ul>
<pre class="ruby-code">
my_inventory = {
"pizza" => 2,
"toy car" => 3,
"book" => 20,
"pencil" => 1,
"TV" => 500
}
my_inventory.map { |item, price| price * 2 }
=> [4, 6, 40, 2, 1000]
</pre>
<ul>
<li>Let's take a look at another example
</li>
<li><code class="ruby-code">my_inventory</code> is a hash (aka an object) and it contains 5 items
along with their prices</li>
<li>We use the map method the same way as the above example except we we use
<code class="ruby-code">item</code> instead of <code class="ruby-code">num</code>, we add an
extra variable called
<code class="ruby-code">price</code> and we added a comma between them to show these are two variables
<li>After that we see a closing veritical bar</li>
<li>Finally we see <code class="ruby-code">price * 2</code> which means we are using each of the items in
<code class="ruby-code">my_inventory</code> and we are multiplying their price with 2</li>
<li>If we use the above codes, we'll see an output of <code class="ruby-code">[4, 6, 40, 2, 1000]</code></li>
</ul>
</section>
<section>
<h2>Map Map! Collect</h2>
<p>
There are 2 ways to use map, there's the map and there's map! (with exclamation). Map will output
results with a new object while map! will change and output results using the existing object.
The terms used to describe this is non-destructive (map) and destructive (map!).
</p>
<p>
Ruby also have another method that behaves just like map called <code class="ruby-code">collect</code>.
Whenever you see the collect method, just know it works the same way as the map method. Learn more
about map method <a href="http://stackoverflow.com/questions/12084507/what-does-the-map-method-do-in-ruby"
target="_blank"> here</a> and <a href="http://ruby-doc.org/core-2.2.0/Array.html#method-i-map" target="_blank">
here</a>.
</p>
</section>
</main>
</div>
</div>
<footer>
<hr>Made from HTML, CSS and coffee © 2016 Bill Deng
</footer>
</body>
</html>