-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
501 lines (283 loc) · 23.5 KB
/
index.html
File metadata and controls
501 lines (283 loc) · 23.5 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Curiouser and Curiouser</title>
<meta name="author" content="Dana Najjar">
<meta name="description" content="
">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://dnajjar.github.io">
<link href="/favicon.ico" rel="icon">
<link href="/assets/bootstrap/css/spacelab.min.css" rel="stylesheet" type="text/css">
<link href="/assets/bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" type="text/css">
<link href="/assets/bootstrap/css/custom.css" rel="stylesheet" type="text/css">
<link href="/assets/font-awesome/css/font-awesome.css" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="Curiouser and Curiouser" type="application/atom+xml">
</head>
<body data-spy="scroll">
<div class="container">
<header class="jumbotron subhead" id="overview">
<div class="subscribe">
<table>
<tr>
<td><span>Get Updates: </span></td>
<td><a href="/atom.xml" class="btn"><i class="icon-cog"></i> By RSS</a></td>
</tr>
</table>
</div>
<h1 class="title">Curiouser and Curiouser</h1>
<p class="lead">A Beginner’s Adventures in Codeland</p>
<div class="navbar">
<div class="navbar-inner">
<div class="container" style="width: auto;">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<div class="nav-collapse">
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/blog/archives">Blog</a></li>
<li><a href="/projects">Projects</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
<form action="https://www.google.com/search" method="get" class="navbar-search pull-left">
<fieldset role="search">
<input type="hidden" name="q" value="site:dnajjar.github.io" />
<input type="text" name="q" results="0" placeholder="Search" class="search-query span2" />
</fieldset>
</form>
</div><!-- /.nav-collapse -->
</div>
</div><!-- /navbar-inner -->
</div>
</header>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2015/05/13/dummy/">Dummy</a></h1>
<p class="meta">
<time datetime="2015-05-13T00:00:00-04:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content">
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2015/04/30/ruby-pass-by-value-or-reference/">Ruby: Pass by Value or Reference?</a></h1>
<p class="meta">
<time datetime="2015-04-30T13:14:23-04:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><p>The confusing part
If you ask the internet whether Ruby is a pass-by-value or pass-by-reference language, it will resoundingly answer that ruby is pass by value. End of story. But if you listen carefully, you can hear a few muffled voices yelling that this is nowhere near the end of the story. The whole truth is that Ruby does things a little differently, and a precise answer would be that Ruby passes arguments by value, and values by reference. Don’t worry, that made no sense to me the first 20 times I read it either. But first things first. What does it mean to pass by reference or value?</p>
<p>When you instantiate a variable, you’re telling your computer that you want to store that variable somewhere in memory.</p>
<blockquote><p>str = “hello”
str.object_id #=> 70219331102480
In this case, we’re creating a variable “str” that points to a location in physical memory where the string “hello” is being kept (the object_id references that location).</p></blockquote>
<p>In Ruby, when you create a second variable and set it equal to the first,
the second variable is now pointing at the same location in memory as the first.</p>
<blockquote><p>str2 = string
str2.object_id #=> 70219331102480
str2 has the same object id as str, which tells us that they’re both pointing at the same object in memory. Here’s a good visualization from Stack Overflow</p></blockquote>
<p>Keep this in mind and we’ll come back to it soon. Let’s switch gears a little bit and think about what happens to a variable when it gets passed to a method. In other words, how is it getting passed? What does the method know about its caller?</p>
<p>Programming languages can handle this in one of two ways: the argument can be passed directly into the method, in which case the method has full access to it. There is no difference between the argument outside the method and the argument inside of the method. This is called passing by reference.</p>
<p>Alternatively, the method can get its own copy of the argument which is identical to the first but points to a different location in memory. This is called passing by value (the method only has access to the value of the argument, not its reference in memory).</p>
<p>So far so good.</p>
<p>So why isn’t this the end of the story? Because Ruby does things a little differently. When a variable calls a method in Ruby, the method is not given full access to that variable. In other words, the variable is not passed by reference. On the other hand, the method is not given its own separate copy, so the variable is not passed by value either. How is this possible? Because the method is given a copy of the variable pointing to the same location in memory. Remember the first thing we talked about? That’s what’s happening (now would be a good time to peek at the drawing). The argument being passed into the method is a copy of the original argument, but points to the same reference in memory.</p>
<p>This has a lot of interesting (and confusing ramifications). Let’s look at some examples.</p>
<blockquote><p>def angry(string)
string = “SO ANGRY”
end
some_string = “warevs”
angry(some_string) #=> “SO ANGRY”
some_string #=> “warevs”
The method didn’t modify the original string, even though it had access to it. How is this possible? Because the method was given a copy of the object, not the object itself. When the method reassigned its copy and told it to point somewhere new in memory, the original object remained blissfully unaware. Let’s try another example.</p>
<p>def please_leave(string)
string.replace(“goodbye”)
end
some_string = “hello”
please_leave(some_string) #=> “goodbye”
some_string #=> “goodbye”
Unlike the first example, the please_leave method changed the argument that got passed into it. WHAT? For the uninitiated, it might look like Ruby is an extremely unreliable language. After all, where would programming be without determinism? You may be able to guess what’s coming next: Ruby’s behavior is still completely deterministic.</p></blockquote>
<p>The Simple Truth
What it all comes down to is the return values of different methods.
Again, what? It’s pretty simple: some methods return new objects with new addresses in memory, and some methods modify their original callers and then return them.</p>
<p>Let’s look at a method that returns an entirely new object than the one that called it.</p>
<blockquote><p>string = “hello”
loud_string = string.upcase #=> “HELLO”
string.object_id #=> 70248691530340
loud_string.object_id #=> 70248691495780
Notice that string and loud_string have different object ids. They are different objects, and are stored in different memory locations.
Now let’s look at a method that modifies its caller and then returns it:</p>
<p>array = [1,2,3]
new_array = array.push(4) #=> [1,2,3,4]
array.object_id => 70248691406240
new_array.object_id => 70248691406240
Notice that array and new_array have the same object ids, even though they are different arrays. The push method reached into the location where array was stored in memory, and changed its contents. It returned the same object, but only after it had modified it. Think of this like reaching into a box, modifying the contents, and then putting it back in its place. You haven’t created a new box, just changed the contents of the existing one. (Just in case that wasn’t clear, the location in memory is the box, the object is its contents).</p></blockquote>
<p>Wrapping it up
To summarize, a method’s copy of its argument is pointing at the same location in memory as the argument itself. If the method is one that returns a new object, the original argument will remain unchanged outside the method. On the other hand, if the method is one that modifies its argument, all copies of the argument will now point at a location whose contents have been modified (think box again.)</p>
<p>How on earth are you supposed to know the return values of every method in Ruby? It will come with practice, and keep in mind that adding a ! to a method tells it to modify the original object instead of returning a new one.</p>
<blockquote><p>string = “hello”
loud_string = string.upcase! #=> “HELLO”
string.object_id #=> 70248691530340
loud_string.object_id #=> 70248691530340
Finally, some objects in Ruby have fixed ids, meaning that they will always point to the same location in memory. Integers, true, false and nil have their own special homes. The integer 5 will point to 11 on Monday, Tuesday and every other day of this week and every week. What this means is that a method will never be able to modify an argument if that argument is one of these special objects, because any modification will automatically be a reassignment. There is no way to reach into an integer’s box and change the contents, because Ruby decided that nothing but that integer can ever be in that box.</p>
<p>def change(num)
num+1
end
num = 5
new_num = change(num) #=> 6
num.object_id #=> 11
new_num.object_id #=> 13</p></blockquote>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2015/04/17/how-to-tell-whether-a-string-is-numeric-in-ruby/">How to Tell Whether a String Is Numeric in Ruby</a></h1>
<p class="meta">
<time datetime="2015-04-17T13:14:23-04:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><p>An issue that’s come up for me several times in my three short weeks of learning Ruby is how to tell whether a string is numeric. In other words, given a string, is there some way to tell whether it’s really just an integer masquerading as a string?</p>
<p>Why, you might ask, would an integer be masquerading as a string in the first place? Identity crises aside, let’s say you build a program that asks the user for input. Let’s use a jukebox command line app as an example. You list all the songs you can play for your user by name and number, and ask them what they’d like to hear. Your code might look something like this:</p>
<blockquote><p>user_input = gets.chomp</p></blockquote>
<p>Let’s say you want to support different kinds of input: your user has the option of giving you a song name, or of choosing a song by number. How your code responds to their input will have to depend on whether they entered a song name - in which case you would need to retrieve the song based on the string they entered - or a song number - in which case you would need to retrieve the song indexed at the number they entered. You might be thinking: why don’t we just call user_input.class and know right away what we’re dealing with? Unfortunately, it’s not that simple. The “s” in “gets” stands for “string”, so anything the user types gets converted into a string. But don’t panic! There are several different ways to tell if your string is really just a number dressed up in fancy quotes. Let’s look at 3 different ways to write an integer? method that returns true if our string is an integer in quotes, and false otherwise. All three can handle positive and negative integers, but only the first (regex way) can handle floats.</p>
<p>The Regular Expressions Way</p>
<blockquote><p>class String <br/>
def integer? <br/>
/\A[-+]?\d+\z/ === self
end
end
This regular expression is giving the following instructions: go to the beginning of the string and look for 0 or 1 “+” or “-” signs, followed by one or more digits, followed by the end of the string. Let’s look at some examples:</p>
<p>“-3”.integer? # => true
“2”.integer? # => true
“one”.integer? # => false
“ISWEARIMANUMBER”.integer? # => false
“12345SURPRISE789”.integer? # => false
If you wanted to update your regular expression to handle floats, then these are the instructions you’d want to give: start at the beginning of the string, look for 0 or 1 “+” or “-” signs, followed by 1 or more integers, followed by 0 or 1 “.”’s, followed by one or more integers, followed by the end of the string. Here’s what that looks like:</p></blockquote>
<p>/^[-+]?[0-9]+.?[0-9]+$/
The Easy Way</p>
<p>I like this method best. It’s simple and elegant, and takes advantage of the fact that you can call to_i on any string in Ruby, and get back “the result of interpreting leading characters in str as an integer” (from www.ruby-doc.org). What this means is that Ruby will look for an integer in your string, and once it finds one, discard any trailing characters. If it doesn’t find any integers, it returns 0. So “1”.to_i is 1, while “one”.to_i is 0, and “99 red balloons”.to_i is 99. Calling to_s on the result will return the original string if and only if it was an integer without any trailing characters.</p>
<blockquote><p>class String
def integer?
self.to_i.to_s == self
end
end
Let’s look at how this works:</p>
<p>my_string = “1”
result = my_string.to_i # => 1
result.to_s # => “1”
result == my_string # => true
my_string = “one”
result = my_string.to_i # => 0
result.to_s # => “0”
result == my_string # => false
Now let’s use the integer? method</p>
<p>“one”.integer? # => false
“1”.integer? #=> true
“99 red balloons”.integer? # => false
The only shortcoming of this method is that while you could write a regular expression to match decimals, calling to_i will always return an integer.</p></blockquote>
<p>The Hard Way</p>
<p>Here’s another way you could do it, though somewhat less intuitive. Calling Integer(str) with a string argument will do one of two things:</p>
<p>1- Return the string.to_i if string.to_i is not 0
2- Throw an error if string.to_i is 0</p>
<p>We ultimately want our integer? method to return a boolean, but all Integer(str) does is either return an integer or throw an error. We can fix that by calling !!Integer(str) and putting that in a begin rescue block that returns false, so that our method returns true if Integer(str) returns an, integer and false if it throws an error. This works because integers are truthy in Ruby, meaning the return value of !int is false, and of !!int is true. Keep in mind that if we didn’t specify a return value of false after the begin rescue block, it would default to nil.</p>
<blockquote><p>class String
def integer?
begin
!!Integer(self)
rescue ArgumentError, TypeError
false
end
end
end
“one”.integer? # => false
“1”.integer? # => true
“1.1”.integer # => false</p></blockquote>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2015/04/06/understanding-git/">Understanding Git: A Beginner’s Guide (Part I)</a></h1>
<p class="meta">
<time datetime="2015-04-06T13:14:23-04:00" pubdate data-updated="true"></time>
</p>
</header>
<div class="entry-content"><p>Note - the first few paragraphs are not git related, so please feel free to skip ahead and ignore my ramblings.</p>
<p>In one of my previous incarnations, I worked as a business analyst at a financial tech startup here in New York. We were a small team, and sharply divided along product and tech lines. I spent my days counting down the clock, and cultivating severe developer envy. I wanted to be on the side that builds things, but I had no idea how to get there.</p>
<p>Anyway, every once in a while I would overhear someone on the tech side say they had submitted a pool request, which invariably evoked the image of a limpid pool in a fairytale clearing in the middle of a lush forest (very specific imagery I know). I had a vague idea that they were all requesting access to this magical pool where all code lives happily ever after. (No, i didn’t think it through.)</p>
<p>It wasn’t until last week that I figured out they had actually been saying pull, not pool. In any case, this post is about git, and my struggle to make sense of it. It’s motivated by a strong suspicion on my part that you could get away with using git for a very long time without actually understanding how it works. Cloning, adding, comitting, and pushing are motions that are simple enough to go through, and there are loads of tutorials on how to set git up and get it working just enough to be lulled into a false sense of competence.</p>
<p>Which is not to say that there aren’t loads of really great resources for learning git - there absolutely are. I just couldn’t find any that tried explain it to me like I was a slower-than-average 5 year old with limited computer knowledge. In that spirit, this post will be very light on the technical sides of git and focus instead on conceptualizing the workflow.</p>
<p>So what is git? git is a version control system. We all know that. But here’s how I wish someone had explained the basics to me from the user’s side of things, without going into under-the-hood mechanics:</p>
<p>You’re sitting at your desk, working on your 3 volume memoir, when it hits you that the paragraph you deleted yesterday was the key to the whole story, and you would give anything to get it back. There should be a way to keep track of changes and be able to get back older versions!</p>
<p>But wait, there is! It’s called git! Wonderful. So you download git, create a github account like everyone tells you to, run git init from the command line to get things going, then realize you have no idea where to go from there.</p>
<p>What happened when you ran git init? A new repository was created in the folder you’re currently in, which you can think of as an empty directory waiting to store whatever you decide to put in it.</p>
<p>Ok. So you get back to work on your novel, and decide you want to store the current version in the new git directory, just in case you need to come back to it later. One catch: your directory doesn’t know about that version yet. You need to create a channel between your version and the git directory by using git add novel. Once that’s done, your git repository becomes aware of your version and you can store a copy of it by saying git commit<em> -m “first novel commit”</em>. The -m lets you add a message so you know what you’ve committed.</p>
<p>So far so good. Everytime you have a working version that you want to save, you can just go through the same process. Let’s say you finished your novel and started proofreading, and want to commit any edits that you make. Running git status will tell you if there are any changes to your file that haven’t been added or committed yet, so you can make sure not to miss anything.</p>
<p>So now you have a repository that stores all past versions of your novel. One catch: the repository is on your laptop, so if you throw it out the window in the throes of an existential crisis, well then that’s the end of that. You need to store the repository remotely! That’s where github comes in. It’s one of many remote repositories you can use to store your commits somewhere safe.</p>
<p>And here’s where things get fancy. You decide to collaborate on your memoir with your long lost evil twin, but the two of you can’t be in the same room together. What’s to be done? You send her a link to your git repository, and tell her to fork and clone it. She is initially offended but eventually understands that this is git speak for creating her own copy of your repository on github and as well as locally. Once she’s done that, she’ll have the most up to date version of your book. But wait! What does it mean for you both to have a copy of the book? What if you both make changes to the same paragraph? Who has the master version? How is this going to be ok?</p>
<p>All that’s for another day folks. Part II coming soon!</p>
</div>
</article>
<div class="pagination">
<a href="/blog/archives">Blog Archives</a>
</div>
</div>
<div class="span sidebar">
<div class="well">
<section>
<h2>Recent Posts</h2>
<ul id="recent_posts">
<li class="post">
<a href="/blog/2015/05/13/dummy/">Dummy</a>
</li>
<li class="post">
<a href="/blog/2015/04/30/ruby-pass-by-value-or-reference/">Ruby: pass by value or reference?</a>
</li>
<li class="post">
<a href="/blog/2015/04/17/how-to-tell-whether-a-string-is-numeric-in-ruby/">How to tell whether a string is numeric in Ruby</a>
</li>
<li class="post">
<a href="/blog/2015/04/06/understanding-git/">Understanding Git: A Beginner’s Guide (Part I)</a>
</li>
</ul>
</section>
</div>
</div>
</div>
</div>
<footer class="footer"><p>
Copyright © 2015 - Dana Najjar -
<span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span> -
<span class="credit">Theme by <a href="http://brianarmstrong.org">Brian Armstrong</a></span>
</p>
</footer>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/assets/bootstrap/js/bootstrap.min.js"></script>
<script src="/javascripts/twitter.js" type="text/javascript"> </script>
<script type="text/javascript">
(function(){
var twitterWidgets = document.createElement('script');
twitterWidgets.type = 'text/javascript';
twitterWidgets.async = true;
twitterWidgets.src = 'http://platform.twitter.com/widgets.js';
document.getElementsByTagName('head')[0].appendChild(twitterWidgets);
})();
$(document).ready(function(){
getTwitterFeed("", , );
});
</script>
</div>
</body>
</html>