forked from otobrglez/webruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter1.html
551 lines (426 loc) · 22.2 KB
/
chapter1.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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<!DOCTYPE html>
<html>
<head>
<title>Programming for the Web with Ruby</title>
<meta charset="utf-8">
<meta name="author" content="Satish Talim" >
<meta name="keywords" content="programming, Ruby, Rack, Sinatra">
<meta name="description" content=
"A Ruby programming tutorial on topics that hopefully will help those that have some knowledge of Ruby programming to get started with web programming.">
<link rel="stylesheet" href="stylesheets/intruby.css" type="text/css">
</head>
<body>
<div>
<p><strong><a href="chapter1.html"><Chapter 1 |</a> <a href=
"toc.html">TOC |</a> <a href="chapter2.html">Chapter 2></a></strong></p>
<h1 class="title">Day 1 - Git, GitHub, RVM, Pik, Gem, Heroku, Bundle (the basics)</h1>
<hr>
<p>To become a Ruby professional, here are some tools that I suggest you
start using as early as possible. Please rest assured that <em>spending
time learning these tools is normal and in the end the reward is worth the
effort</em>.</p>
<h2>Using Git</h2>
<p>Here's what a <a href=
"http://rubylearning.com/blog/2009/01/13/little-known-ways-to-rubymastery-by-thibaut-barrere/">
Ruby master</a> says about Git - "A new Ruby programmer should focus on
source control - even if you work alone, even for a small "labs" type of
applications, use SVN, Git or whatever you feel comfortable with."</p>
<h3>What's Version Control?</h3>
<p><em>Version control</em> also known as source control, source code
management or revision control in its simplest form is saving your first
draft, and then saving it as draft 2 and continuing on with your editing.
This allows you some means of going back to a previous version if you
decide that the direction you took in your document is not the direction
you want to go, and you want to continue on from where your first draft
originally was. Version control in a more granular manner is being able to
see each change as it was made. In reality, it ends up being likely that
you want something between these two extremes.</p>
<p><a href=
"http://en.wikipedia.org/wiki/Version_control_system#Common_vocabul">Read
the Wikipedia article on version control</a> for more details.</p>
<h3>What's Git?</h3>
<p>Git is an open source version control system designed to handle very
large projects with speed and efficiency, but is just as well suited for
small personal repositories (collection of resources that can be accessed
to retrieve information); it is especially popular in the open source
community, serving as a development platform for projects like the Linux
Kernel, Ruby on Rails, etc.</p>
<h4>Downloading and Installing Git</h4>
<p>To download and install Git, the precompiled packages are available
here: <a href="http://git.or.cz/">http://git.or.cz/</a></p>
<p>Select the relevant package for your operating system.</p>
<p><b>Note:</b> Git still has some issues on the Windows platform but for
normal usage the msysgit package shouldn't let you down.</p>
<p>Download and install it from the url: <a href=
"http://code.google.com/p/msysgit/downloads/list">http://code.google.com/p/msysgit/downloads/list</a>
and select the current version available.</p>
<p>Install by running the EXE installer. Accept the default install
directory. It is recommended for Windows users to use the "Use Git Bash
only" option.</p>
<h4>Create a local folder</h4>
<p>Create a new empty folder in any convenient location on your hard disk
(I created and use the folder <code>my_ruby_programs</code> on c: of my
hard disk).</p>
<h4>Let us start using Git</h4>
<p>Since I work on a Windows box, I shall talk about how to use Git there.
However, all the following commands are also available on all other
platforms.</p>
<p>In Windows Explorer, right mouse click on your local folder
<code>my_ruby_programs</code> and choose "Git Bash Here". This opens up a
command window (bash shell).</p>
<h4>Introduce yourself to Git</h4>
<p>For all operating users, you now need to identify yourself to Git (you
need to do this only once). With the bash shell still open type in the
following:</p>
<pre>
$ git config --global user.name "Your name here"
$ git config --global user.email "Your email id here"
</pre>
<p>Substitute in your own user name and email (note that Git does not allow
accented characters in user names). This will set the info stored when you
commit to a Git repository. Git has now been set up.</p>
<h4>Create your SSH Key</h4>
<p>The first step in using Git is to create your SSH Key. This will be used
to secure communications between your machine and other machines, and to
identify your source code changes. (If you already have an SSH key for
other reasons, you can use it here, there is nothing Git-specific about
this.)</p>
<p>To create a new ssh key, type the command:</p>
<pre>
$ ssh-keygen -C "[email protected]" -t rsa
</pre>
<p>(with your own email address, of course).</p>
<p>Accept the default key file location. When prompted for a passphrase,
make one up and enter it. If you feel confident that your own machine is
secure, you can use a blank passphrase, for more convenience and less
security. Note where it told you it stored the file. On the machine I
tested with, it was stored in "c:\Documents and Settings\A\.ssh\".
<em>Memorize your passphrase carefully. If you forget it, you will NOT be
able to recover it</em>.</p>
<p>Open the public file <code>id_rsa.pub</code> with a text editor. The
text in there is your "public SSH key".</p>
<h2>Using GitHub</h2>
<h3>What's GitHub?</h3>
<p>GitHub is a social code site optimized for hosting and sharing Git
repositories. Putting a copy of your Git repository at GitHub serves two
purposes:</p>
<ol>
<li>it's a full backup of your code (including the full history of
commits).</li>
<li>it makes any future collaboration much easier. This step is optional,
but being a GitHub member will open the door to participating in a wide
variety of open-source projects.</li>
</ol>
<h4>Set up your GitHub account</h4>
<p>Go to <a href=
"https://github.com/signup/free">https://github.com/signup/free</a> and
sign up for a free account. Remember, this account can have unlimited
public repositories and public collaborators (the total number of users who
may read, write and fork your public repositories). Also, make sure that
Javascript is enabled in your browser.</p>
<p>After you have created a GitHub account, please login and click on "Edit
Your Profile" and fill in the relevant details in your GitHub account.</p>
<h4>Creating a new repository</h4>
<p>If you are already logged into your GitHub account you just created
above, click on the "Dashboard" link at the top right-hand-side of your
GitHub page, you will see a "New Repository" button. Click on "New
Repository" to create a new public repository (fill in the details like
project name, description etc.) Use the name <code>my_ruby_programs</code>
for your Project name.</p>
<p><b>Important:</b> GitHub will show you a page with some instructions
like Global setup, Next steps etc. - please keep this page open and get
back to your already open Bash shell for the folder
<code>my_ruby_programs</code>. Currently your folder
<code>my_ruby_programs</code> is empty.</p>
<p>Next we shall put our local folder <code>my_ruby_programs</code> under
Git management. In the open Bash shell type:</p>
<pre>
$ git init
$ touch README
$ git add README
$ git commit -m 'first commit'
</pre>
<p>The <code>git init</code> command creates an empty Git repository or
reinitializes an existing one. It has created a repository in the current
folder i.e. <code>my_ruby_programs</code>. Git has created a
<code>.git</code> subdirectory in your folder <code>my_ruby_programs</code>
that will hold your changes locally (in compressed form). The new local
repo will not contain anything yet.</p>
<p>The <code>touch README</code> command creates the file
<code>README</code>, if the file does not already exist. If the file
already exists, the access or modification time is updated for that
file.</p>
<p>In Git, you "stage" things before you commit them. You do this with the
<code>git add</code> command (for example: <code>git add README</code>).
This adds specific content to the "staging area". To stage all changes and
new files, we type <code>git add .</code></p>
<p>The <code>git add .</code> command will take the working directory and
all subdirectories and every single file, i.e. it adds all content to the
"staging area" (this snapshot is now stored in a temporary staging area
which Git calls the "index"). If you make any changes to a file after
staging (but before committing), you'll need to <code>git add</code> the
file again.</p>
<p>When you get your changes just the way you want them added to the
current revision, then you need to commit that revision to your local
repository (this permanently stores the contents of the index in the
repository).</p>
<p>We already have something staged and ready and it is time to commit it.
We do it as follows <code>git commit -m 'first commit'</code>.</p>
<h4>Add your SSH key to GitHub</h4>
<p>Access your account on GitHub and Click "Account Settings" > Click
"SSH Public Keys" > Click "Add another public key"</p>
<p>Open the <code>id_rsa.pub</code> file (which we had talked about
earlier) with a text editor (Notepad, TextEdit, or gedit will do just
fine). This is your public SSH key. You may need to turn on "View hidden
files" to find it because the .ssh directory is hidden. It's important to
copy your SSH key exactly as it is written without adding any newlines or
whitespace. Now paste it into the "Key" field. Hit "Add Key".</p>
<p>In your open Bash shell, type:</p>
<pre>
$ git remote add origin [email protected]:SatishTalim/myrubyprograms.git
</pre>
<p>Replace SatishTalim (use your username) with what's shown on the already
open GitHub page. The above command needs to be executed only once in case
you are not going to change the remote repository.</p>
<p>Now type:</p>
<pre>
$ git push -u origin master
</pre>
<p>Once your changes are committed to your local repository, you need to
push them to the remote repository for others to get at. To do that, you
need to execute <code>git push</code>, which will push all the changes from
your local repository up to the remote repository. We want to push changes
back to the original repository, which is aliased as <em>origin</em>, to
the master branch.</p>
<p>Here's my sample GitHub repository - <a href=
"https://github.com/SatishTalim/myrubyprograms">https://github.com/SatishTalim/myrubyprograms</a>.</p>
<p><b>Note:</b> From this point onwards, whenever you create a new program,
or modify an existing program in the folder <code>my_ruby_programs</code>,
open a Bash shell in that folder and type:</p>
<pre>
$ git add .
$ git commit -a -m "a note about your commit"
$ git push
</pre>
<p>The GitHub repository will show you the latest files that you have
uploaded.</p>
<p>We shall be using Git and GitHub throught this course.</p>
<h2>Using RVM (for *nix)</h2>
<p><b>Credit:</b> <a href="https://github.com/punjab">Arvinder Singh Kang</a></p>
<p>One of the problems that many newbies in a language face while following
a book or tutorial is the problem of dealing with different versions of the
language e.g. version 1.8.7 and version 1.9.3 in the case of Ruby. If you
are running <b>Linux or OS X</b>, do not worry - we have got
<code>rvm</code> power!</p>
<h3>What is RVM?</h3>
<p><a href="http://beginrescueend.com/" title=
"RVM: Ruby Version Manager">Ruby enVironment (Version) Manager (RVM)</a> is
a command-line tool developed by <a href=
"https://github.com/wayneeseguin">Wayne E. Seguin</a> which allows you to
easily install, manage, and work with multiple Ruby environments from
interpreters to sets of gems. You can install and separately manage more
than one Ruby interpreter including MRI, JRuby and the kind.</p>
<h3>Pre-requisites</h3>
<p>Most Linux/OS X using <code>bash</code> will have the necessary tools,
including <code>git</code> - required to install RVM. However you can check
the location of installed tools by typing this at your <code>bash</code>
prompt:</p>
<pre>
for name in {bash,awk,sed,grep,ls,cp,tar,curl,gunzip,bunzip2,git,svn} ; do which $name ; done
</pre>
<p>If any of the tool is missing, you may install the missing package using
<code>apt-get</code> in Linux or using <a href=
"http://mxcl.github.com/homebrew/">Homebrew</a> on OS X.</p>
<h3>Installing RVM</h3>
<p>The simplest way to install <code>rvm</code> is using the
<code>rvm-installer</code> script, by typing the following at your
<code>bash</code> prompt:</p>
<pre>
bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
</pre>
<p>If you don't trust the installer script, you can follow the instructions
given <a href="http://beginrescueend.com/rvm/install/">here</a> and
download and inspect the script before installing.</p>
<h3>Loading RVM into your shell</h3>
<p>To make sure you can use <code>rvm</code> as a function or command from
your <code>bash</code> shell, add the following at the end of your
<code>.bash_profile</code> file located in your home folder.</p>
<pre>
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
</pre>
<h3>Reload shell configuration and test</h3>
<p>The simplest way to reload a shell is to close and restart it. If you
are lazy like me and don't want to do that, try reloading your
<code>.bash_profile</code></p>
<pre>
source .bash_profile
</pre>
<p>Now let's test if all went well:</p>
<pre>
type rvm | head -1
</pre>
<p>If all went well, you should see this response:</p>
<pre>
rvm is a function
</pre>
<h3>Install a Ruby interpreter</h3>
<p>To see a list of Ruby interpreters available to us, run this
command:</p>
<pre>
rvm list known
</pre>
<p>You should see a list of Ruby interpreters available to be installed.
For the purpose of this class, we'll install Ruby 1.9.3</p>
<pre>
rvm install 1.9.3
</pre>
<p>Once the installation completes, let's give this new Ruby a spin:</p>
<pre>
rvm use 1.9.3
ruby -v
</pre>
<p>You should see something like the following depending on your OS and
installed version:</p>
<pre>
ruby 1.9.3p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
</pre>
<p>To load this version as our default version of Ruby, run the following
command:</p>
<pre>
rvm use 1.9.3 --default
</pre>
<p>Thats all! Enjoy creating fun in Ruby.</p>
<h2>Using pik (for Windows)</h2>
<h3>What's pik?</h3>
<p>Pik is a tool to manage multiple versions of Ruby on Windows. It can be
used from the Windows command line (cmd.exe) or Git Bash.</p>
<h3>Installing pik</h3>
<p>My machine already has Ruby 1.9.3 installed which means I can install
pik via RubyGems. If you don't already have Ruby, the README file on GitHub
has a section called "<a href="https://github.com/vertiginous/pik">Install
pik using the installer</a>" which I recommend you follow.</p>
<pre>
gem install pik
</pre>
<p>After downloading the gem, you need to install pik into a directory on
your machine. This can be anywhere, apart from where you already have Ruby
installed.</p>
<pre>
mkdir pik
</pre>
<p>You need to include the directory used above in your %PATH% system
environment variable before the location of any existing Ruby installation.
I put it at the start.</p>
<p>When installing, simply specify the directory you picked.</p>
<pre>
pik_install c:\pik
</pre>
<p>After which, you will have three files in the folder. This is everything
required for pik.</p>
<pre>
01-Dec-11 09:25 AM 119 pik.bat
01-Dec-11 09:25 AM 145 pik.ps1
01-Dec-11 09:25 AM 694,272 pik_runner.exe
</pre>
<h3>Using pik</h3>
<p>Executing <code>pik add</code>, adds all the Ruby locations to pik.</p>
<pre>
pik add
** Adding: 193: ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
Located at: C:\Ruby193\bin
* 193: ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
</pre>
<p>Executing <code>pik list</code> outputs all the Ruby installations it
knows about.</p>
<pre>
pik list
* 193: ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
</pre>
<p>Executing <code>pik install [options]</code> downloads and installs
different Ruby versions. Choices are: ruby, jruby, or ironruby. If no
version is specified, the latest version will be installed.</p>
<p>Download and install locations can be configured with <code>pik
config</code>. Examples:</p>
<pre>
# install the latest version of JRuby (currently 1.4.0RC1)
pik install jruby
# install the latest 1.8 version of MinGW Ruby
pik install ruby 1.8
</pre>
<p>Using pik, I installed jruby:</p>
<pre>
pik install jruby
** Downloading: http://jruby.org.s3.amazonaws.com/downloads/1.6.5/jruby-bin-1.6.5.zip
to: C:\Users\TALIM\.pik\downloads\jruby-bin-1.6.5.zip
</pre>
<p>I extracted the jruby-bin-1.6.5.zip file and installed jruby.</p>
<p>I now need to tell pik about my installation by pointing it at the bin
directory. That's it.</p>
<pre>
pik add C:\jruby-1.6.5\bin
** Adding: 165: jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM) Client VM 1.6.0_29) [Windows 7-x86-java]
Located at: C:\jruby-1.6.5\bin
</pre>
<p>If you <code>list pik</code>, then you can see all the different
versions installed:</p>
<pre>
pik list
165: jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM)...
* 193: ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
</pre>
<p>You can then switch to the particular version you want, for example
originally I was running 1.9.3.</p>
<pre>
E:\pik>ruby -v
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
</pre>
<p>But I can then switch to 1.6.5 with a simple command.</p>
<pre>
pik switch 165
ruby -v
'ruby' is not recognized as an internal or external command, operable program or batch file.
jruby -v
jruby 1.6.5 (ruby-1.8.7-p330) (2011-10-25 9dcd388) (Java HotSpot(TM) Client VM 1.6.0_29) [Windows 7-x86-java]
</pre>
<h2>What's a Ruby Gem?</h2>
<p>To solve various problems with Ruby, you might develop your own libraries. Also, you might want to open-source your libraries to get help from the Ruby community and have many developers working on the same.</p>
<p>A gem is a packaged Ruby application or library. RubyGems is the standard way to distribute Ruby applications and libraries and is available to you after you have downloaded and installed Ruby 1.9+.</p>
<h2>What's Heroku?</h2>
<p>Heroku (pronounced her-OH-koo) is a cloud application platform - a new way of building and deploying web apps.</p>
<h3>Create an account on Heroku</h3>
<p>Please ensure that you are connected to the internet and then create an account on Heroku (obviously do this only once) if you don't have one - <a href="http://heroku.com/signup">http://heroku.com/signup</a>.</p>
<p>On your local machine, open a new command window to install the Heroku gem file. Type:</p>
<pre>gem install heroku
</pre>
<p>Next, create a new folder, say <code>rackheroku</code>. Assuming that you have Git installed, open a Bash shell in that folder.</p>
<p>Upload your public key (do it only once):</p>
<pre>$ heroku keys:add
</pre>
<p>You'll be prompted for your username and password the first time you run a heroku command; they'll be saved on ~/.heroku/credentials so you won't be prompted on future runs. It will also upload your public key to allow you to push and pull code.</p>
<h2>Install Bundle</h2>
<p>In the already open command window, we will install bundler. Type:</p>
<pre>gem install bundler
</pre>
<p>Close the command window.</p>
<p>Bundle allows you to bundle your gems (both pure-ruby and native) with your application.</p>
<h2>Exercise: URL of your GitHub repo</h2>
<p>Please create and post the URL of your GitHub repo in the relevant
forum, when the course starts.</p>
<h2>Fun Exercise: Watching Repos</h2>
<p>It's a good idea to watch some interesting repositories. At the top of your GitHub page, click on the Explore button. You will be taken to a page that shows you "Trending Repos" and "Featured Repos". Select and click on the repo you are interested in. You will be taken to that repo screen. Next, click on the "Watch" button. This repo will now appear in your "Watched Repositories". Once you start watching a repo, then anytime anything is committed to this or the wiki is changed or something, you will be able to see that in your personal News Feed or you will be notified if you have subscribed to this RSS feed. In the technical Forum of the course, please post the URL's of the repos that you are interested in and watching. Have fun!</p>
<h2>Using Git & GitHub eBook (Updated 15th Sept. 2011)</h2>
<p>For a more detailed look at Git and GitHub, you could consider buying
the <a href="http://rubylearning.com/blog/using-git-github-ebook/">Using
Git & GitHub eBook</a>, an easy-to-follow guide to learn the essential
features of both Git and GitHub that you'll end up using every day.</p>
<p>It is priced at a reasonable <b>US$ 7</b>. The money collected helps me
maintain the site and provide quality content to you all.</p>
<p>Purchase of this eBook is <b>not mandatory</b> for this course.</p>
<p><strong><a href="chapter1.html"><Chapter 1 |</a> <a href=
"toc.html">TOC |</a> <a href="chapter2.html">Chapter 2></a></strong></p>
<div id="footer">
<p>© 2006-2012 <strong>RubyLearning.org - Programming for the Web with Ruby</strong> Page Updated: 21st Jan. 2012</p>
</div>
</div>
</body>
</html>