forked from otobrglez/webruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter4.html
141 lines (119 loc) · 5.81 KB
/
chapter4.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
<!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">
</head>
<body>
<div>
<p>
<strong>
<a href="chapter3.html"><Chapter 3 | </a>
<a href="toc.html">TOC | </a>
<a href="chapter5.html">Chapter 5></a>
</strong>
</p>
<h1 class="title">Day 4</h1>
<h2>Creating one's own Ruby Gem</h2>
<h3>Let us create a simple Ruby library</h3>
<p>Let's first create a simple library that extends the <code>String</code> class and store this in a file called <code>my_string_extend.rb</code> in the folder <code>my_string_extend/lib</code>. The <em>lib</em> folder will contain the Ruby code related to the library:</p>
<pre># my_string_extend.rb
class String
def writesize
self.size
end
end
</pre>
<p>The library opens up the <code>String</code> class and adds a method <code>writesize</code> (which returns the size of the string).</p>
<p>For open-source applications, the gem server to use for your applications is the one provided at <a href="https://rubygems.org/">https://rubygems.org/</a>. This is where users will look, and is where gem will also look by default when you issue a <code>gem install</code> command. Please make sure that the name of your gem isn't in use already. Type:</p>
<pre>$ gem list -r my_string_extend
*** REMOTE GEMS ***
</pre>
<p>Observe that no gem of the name <code>my_string_extend</code> exists. We are safe! Now let us now turn our library into a gem, so that one can use it anywhere.</p>
<h3>Steps for publishing our gem</h3>
<p><b>1.</b> Update to the latest RubyGem - In the folder <code>my_string_extend</code> open a command window and type:</p>
<pre>$ gem update --system
</pre>
<p><b>2.</b> Create an account on RubyGems.org - <a href="http://rubygems.org/sign_up">http://rubygems.org/sign_up</a> and confirm your email. Later on, you'll need to provide your login credentials the first time you push a gem to the server.</p>
<p><b>3.</b> Create a Gem Specification - In the folder <code>my_string_extend</code> copy the following file <code>my_string_extend.gemspec</code> and edit it as per your needs:</p>
<pre>Gem::Specification.new do |s|
s.name = 'my_string_extend'
s.version = '0.0.1'
s.date = '2011-10-27'
s.summary = "String Extend"
s.description = "The library opens up the String class and adds a method writesize, which returns the size of the string."
s.authors = ["Satish Talim"]
s.email = ["[email protected]"]
s.homepage = "http://rubylearning.org/"
s.files = ["lib/my_string_extend.rb"]
end
</pre>
<p>The gemspec defines what's in the gem, who made it, and the version of the gem. The gemspec is a chunk of Ruby code that the gem command will read and execute to understand our app, such as its name, files, and dependencies. It's also our interface to RubyGems.org, all the information you see on a gem page comes from the gemspec.</p>
<p><b>4.</b> Once we have our gemspec, we have to build a gem from it. In the folder <code>my_string_extend</code> type:</p>
<pre>$ gem build my_string_extend.gemspec
Successfully built RubyGem
Name: my_string_extend
Version: 0.0.1
File: my_string_extend-0.0.1.gem
</pre>
<p>We can then install it locally to test it out:</p>
<pre>$ gem install ./my_string_extend-0.0.1.gem
Successfully installed my_string_extend-0.0.1
1 gem installed
Installing ri documentation for my_string_extend-0.0.1...
Installing RDoc documentation for my_string_extend-0.0.1...
</pre>
<p>Lets require our gem and use it:</p>
<pre>$ irb --simple-prompt
>> require 'my_string_extend'
=> true
>> puts "Hello".writesize
5
=> nil
>> quit
</pre>
<p><b>5.</b> Now publish our gem out to RubyGems.org. In the folder <code>my_string_extend</code> type:</p>
<pre>$ gem push my_string_extend-0.0.1.gem
Enter your RubyGems.org credentials.
Don't have an account yet? Create one at http://rubygems.org/sign_up
Email: [email protected]
Password:
Pushing gem to https://rubygems.org...
Signed in.
Pushing gem to https://rubygems.org...
Successfully registered gem: my_string_extend (0.0.1)
</pre>
<p>In just a few moments (usually a minute), your gem will be available for installation by anyone:</p>
<pre>$ gem list -r my_string_extend
*** REMOTE GEMS ***
my_string_extend (0.0.1)
</pre>
<p>Here's our Ruby gem on the RubyGems.org site</p>
<div style="width:image 560 px; font-size:80%; text-align:center;"><a href="https://rubygems.org/gems/my_string_extend"><img src="images/rubygem.jpg" alt="My gem" width="560" style="padding-bottom:0.5em;" /></a><br />Our Ruby Gem</div>
<h2>Exercise - Create your gem</h2>
<p>Based on what you have learnt so far, create a gem for the code below and publish the same to RubyGems.org Once done, post the URL of your gem in the technical forum. Please name the gem as rl_hiya_lastname_firstname</p>
<pre>class Hiya
def self.hiya
puts "Hiya all!"
end
end
</pre>
<h2>Deleting your gem at Rubygems.org</h2>
<p>If for some reason you would like to remove your gem from RubyGems.org’s index, then use the <code>DELETE</code> command as mentioned <a href="http://guides.rubygems.org/rubygems-org-api/#gem">here</a>.<p>
<p>
<strong>
<a href="chapter3.html"><Chapter 3 | </a>
<a href="toc.html">TOC | </a>
<a href="chapter5.html">Chapter 5></a>
</strong>
</p>
<div id="footer">
<p>© 2006-2012 <strong>RubyLearning.org - Programming for the Web with Ruby</strong> Page Updated: 26th Jan. 2012</p>
</div>
</div>
</body>
</html>