Beginning Ruby
It seems that my advocacy and persistence has caused a stir in the local crowd. After the launch of the Malaysia Ruby Brigade, I seem to be picking up more and more followers, and a lot of them are new to Ruby and interested in learning more. So I thought I'd write a post on Beginning Ruby.
I personally think that Ruby has at the same time a high barrier and low barrier of entry. For people new to programming, it's a very simple language to learn, but if you're coming from a programming background, there are some perceived conventions that you'll have to overcome.
I was never much into reading programming books, about 99% of the ones I pick up, I stop after reading 25% of it.
Why you ask? Because I
- get bored
- had itchy fingers
- feel that I know enough and can start hacking on my own
Because of that I'll go over the interactive introductions to Ruby first over the books, because well I never finished reading them. Because of this I also don't tend to buy programming books. This list is intentionally kept short so that it provides a nice variety of sources, but does not flood the user with so many possibilities that he doesn't know where to start.
These are also the projects that I believe are the best resource in learning programming Ruby.
Official Ruby Site

The official Ruby site is the place of congregation for all things Ruby related. I personally think its a beautiful site. The site has a lot of resources for you to get started on Ruby.
Here are some recommended readings from their site, that I won't go over:
Interactive
Try Ruby!

Before you even install Ruby, why don't you Try Ruby! first in your Web browser. No need to worry about how to set up Ruby, or which text editors to use, or even destroying your System in the process. Try Ruby! is an excellent way to well, try it without worrying about the hassles with installing and setting up Ruby. It even comes with a 15 minute tutorial to get you started.
Try Ruby! is actually the interactive ruby (irb) application that comes with Ruby. It allows you to use Ruby from the command line. Nifty for experimenting and debugging code.
So what happens when you've grown tired of Try Ruby! and want more? Don't worry, from the same creator of Try Ruby! comes Hackety Hack: The Coder's Starter Kit.
Hackety Hack: The Coder's Starter Kit
Hackety Hack (or sometimes referred to as HH) is the latest work of why (yes as in his name), bringing with him his colorful personality into a world. At the moment Hacket Hack is still in beta. The goal of Hackety Hack is to act as an introduction into programming, and the choice of language used is Ruby. Sadly at the moment it is only available on Ubuntu, Windows XP and Windows Vista.
In the attempt to lower the barrier of entry into programming, by default, installing Hackety Hack, you also install Ruby making it very easy to new programmers. Interestingly enough the programs you create are also referred to as "hacks". You don't use an IDE at all, or a text editor. You code within the Hackety Hack program itself, and it saves your "hacks" into a folder. You write Hackety Hack also uses its own set of libraries to let you do things quickly. For example, parsing feeds, doing web popups, etc.
Hackety Hack comes with its own forums, Talkety Talk, to facilitate discussions within the community. But an even nicer feature is the ability to 'share hacks'. When you register on the forum, you are given a workspace where you can upload your hacks and tables via Hackety Hack. Mine is a bit empty at the moment. Have a look at why's.
I've previously written another post regarding Hackety Hack before.
I personally find Hackety Hack a nice introduction into both programming and Ruby.
Reading
Ruby in Twenty Minutes
How about trying Ruby in Twenty Minutes. This requires that you have interactive ruby (irb) installed on your computer. But don't worry, you can try these out in the Try Ruby!. You may not notice but it is actually four pages long. The pagination is located at the title.
Why's (Poignant) Guide to Ruby

Another of why's project is the Why's (Poignant) Guide to Ruby. I'd consider this a radical book in that it approaches programming in such a different way that it really stand out. It is unique in that sense. It stands out because it is highly illustrated with bizarre comics.
I personally found it difficult to follow the Poignant Guide because it was just so bizarre. Though I do know other people follow it religiously. It may not be for me, but if you are up to it. Do have a read.
It is also available in PDF, for easy reading and printing, a soundtrack, and a purchasable a book.
The book and its materials are also licensed under the Creative Commons By Attribution Share Alike 2.5 License. So you are pretty much free to do whatever you want with the book, as long as the license is the same.
Programming Ruby

Programming Ruby (otherwise known as the Pickaxe book) is the original book I followed when learning Ruby (though I never finished reading it). Its a bit outdated (for use with v1.6 of Ruby), but alot of it is still applicable. A newer version is available, though its not available on the Internet. For a currently established Programmer, I found this book to be a great straight forward introduction into Ruby.
Ruby Cookbook
I find the O'Reilly Ruby Cookbook real handy as it gives practical examples to get things done quickly with Ruby. For example, setting up database connections, graphics manipulation, web services, automating tasks and more.
Other Resources?
Do you think there are other worth while resources that should be here? Please do recommend some then, and I'll have a look at them.
Note: Note: There are alot and a growing number of Ruby resources out there. This post will be updated as I see fit.
The Importance of Naming
What's in a name? That which we call a rose, By any other word would smell as sweet;
Juliet, Scene II, Romeo and Juliet1
Names make it easier for us to identify both physical objects and abstract ideas and feelings.
As names are important to us, they should not be abused. The same goes with sentences. Adjectives, verbs, nouns, everything comes together to piece a cohesive sentence. Conventions are set in place to ensure that everyone can understand your sentence.2
The same goes with code. Programming languages come with conventions to make your code look readable3, beautiful, poetic, reusable, and hence save you alot of time in coding.4
Code conventions can be top down, and dictated by the Project Manager, or be recommendations from the bottom up.
This post will look at some of the stylistic conventions or syntatic sugar that Ruby5 provides to make life easier for you.
Emo Methods!6
When starting Ruby, a lot of things took me back. Especially the unique inclusion of allowing methods to be named with the exclamation and question mark.
I absoutely love
this feature of Ruby, as it gives the language expression.
When I think of this feature, I think of Newspeak, in that our ability to express oneself is only limited to our vocabulary. A larger vocabulary leads to a more expressive language7, and more expression allows one to state their intent clearly.
The Question Mark (?)
Oh Magical Eight Ball
is this array empty?
array = [] # Declaring an Array array.empty? # true
Magical Eight Ball
responds: Yes — definitely.
Methods with a question mark, are intended to "ask a question" (obviously), and are intended to only return true or false (unlike a Magic Eight Ball [image16x16:8ball] with 20 different possible variations). These methods generally involve looking at what state the object is currently in.
Because everything is an object in Ruby, the context is very clear. You are testing on an Array, and not a Numeric.
In other languages that support dynamic arrays, it may look something like this: (In PHP)
$array = array(); # Declaring an Array count($array) == 0 # true
Magical Eight Ball
responds: Reply Hazy, try again.
What am I doing? Oh poor lament souls what does count do?
The Exclamation Mark (!)
Methods ending in a exclamation mark indicates that the method is destructive (in that it will modify itself). Beware, the object will cut it self!
string = " I am protected by whitspace! " # Declare a string string.strip # "I am protected by whitespace!" string # " I am protected by whitespace! " string.strip! # "I am protected by whitespace" string # "I am protected by whitespace"
Magic Eight Ball
responds: Don't count on it.
Here we declare a string, and try to strip it of its whitespace. The second line calls the strip method where the return value's whitespaces have been removed from both the left and right side. But when you check the variable again (line 3), the dreaded whitespace still exists! You are safe for now little string. This time we specify an exclamation mark (line 4), and check the value (line 5) Destructive!
Lets go through another (more complicated) example, looking at how each object has a different id.
string = "I don't want to be MODIFIED" string.object_id # -438 lowercased = string.downcase # "i don't want to be modified" lowercased.object_id # -498 <= Freak string.object_id # -438 newstring = string.downcase! # "i don't want to be modified" newstring.object_id # -438 string.object_id # -438 newstring # "i don't want to be modified" string # "i don't want to be modified"
Magic Eight Ball
responds: My sources say no.
In this example, we will once again be using the downcase method to down/lowercase the string. Without the use of the exclamation mark (line 4), a new object is returned (see the object_id). When used with the exclamation mark (line 6), the returned object, is the object itself.
Note:
object_idis the integer identifier of the object in program. No two objects share the same object_id.- You don't need to collect it into another variable, I just put it there for demonstration pursepose.
What is the purpose of including these? It makes it easier for the programmer to judge the intention of the method.
Capital Letters Please
Ruby has a naming convention for constants and classes. These should begin with a capital letter, and follow any series of alphanumeric characters or the underscore. There are no other way to define constants. define() or #DEFINE does not exist in the syntactical world of Ruby.
MAXIMUM_PAIN = 100
Magic Eight Ball
responds: It is certain.
For word delimiters, constants use an underscore (RUBY_VERSION), and classes use camelcase (ActiveRecord).
Here is an example (done within irb) of trying to declare a class that doesn't start with an uppercase letter.
>> class terror
>> end
SyntaxError: compile error
(irb):5: class/module name must be CONSTANT
from (irb):2
from :0
Magic Eight Ball
responds: Outlook not so good.
Bad. Ruby simply complains! You are doing a bad thing! And bad things come from bad people! Therefore you are a bad person and should be condemned to the fires of SyntaxError Hell! We are not writing Manglish people.
Conclusion
Anyways, just some tips to keep your code looking beautiful throughout the years. 
Out of curiosity, are you a "undisciplined_underscore_utopian" or a "CamelCaseConspirator"?
Note:
- This is one of the few lines I can recall from Shakespeare's plays
- Thats right, I'm looking at you Manglish speaking people.
- Coding conventions, do not guarantee readable code. You can still make spaghetti code following these conventions.
- Actually Ruby has many many ways to do one thing. This is both a pro and a con.
- These conventions doesn't mean Ruby is rigid.
- Or other wise known as Descriptive Methods
- Of course its up to the person to be familiar with the vocabularly
Taking A Peek at HTML 5
The Editor's draft of the HTML5 difference from HTML4 is up, and there are some interesting things. So lets have a look at what I might be bitten by, or find useful.
Warning: I am not familiar with how the workings of HTML5, so the examples shown below shown may not be accurate, but are intended to show the possibility with the new specification.
Stricter Content Models
This one is interesting.
New Elements
Wow, talk about a large number of new elements (brief description of each tag) for the HTML specification.
Some notable tags:
So now we can define headers, footers and navigation without relying on a id attribute. Section would also be good as it provides a semantic approach into "sectioning" elements in a page.
So for a blog, you can have "sections" for different posts.
<article>
<header>
<h1>Post Title</h1>
<p><time datetime="2007-06-17">17th June 2007</time></p>
</header>
<p>Post Content</p>
<footer>
Posted in
<ul>
<li> Category 1 </li>
<li> Category 2 </li>
<li> Category 3 </li>
</ul>
</footer>
<article>
The progress tag is great for Ajax applications where you would like to show the progress of your query.
The meter tag is excellent for data, and would fit well into tabular data as well!
The meter element represents a scalar measurement within a known range, or a fractional value; for example disk usage, the relevance of a query result, or the fraction of a voting population to have selected a particular candidate.
Additional types for input
Woot! It seems like the input element is getting additional types.
Here they are:
- datetime
- datetime-local
- date
- month
- week
- time
- number
- range
- url
Would make user side validation alot easier.
New Attributes
The new
requiredattribute applies toinput(except when the type attribute ishidden,imageor some button type such assubmit) andtextarea. It indicates that the user has to fill in a value in order to submit the form.
This would make things a hell lot easier for people specifying require attributes. Additionally it would check on the user side as well. (I hope so at least).
So using CSS and the after pseudo attribute you can add the (common) asterisk "*" indicating the field is required.
CSS:
input[required]:after { content: '*' }
HTML:
<input type="text" required />
This could be done before using a class, but its nice that its an attribute now.
You can now disable an entire fieldset by using the disabled attribute on it. This was not possible before.
Makes it easier to disable elements! Yay!
Manipulating HTMLElements made even easier
The function getElementsByClassName() included by default — Major wootage!
But even better is that a new accessor classList is defined for HTMLElements, making our job in manipulating classes very easy!
classList is a convenient accessor for className and the object it returns exposes methods, such as has(), add(), remove() and toggle(). The a, area and link elements have a similar attribute called relList that exposes the same for the rel attribute.
No need to rely on Javascript libraries to manipulate the CSS easily!
var phpcode = document.getElementsByClassName('phpcode');
for(var i = 0; i < phpcode.length; i++) {
phpcode[i].classList.remove('phpcode');
phpcode[i].classList.add('rubycode');
}
Browser Support?
Now the big question? Which browsers support HTML 5?
At least My Missing Quicktags plugin is ready to enter any of these tags into WordPress if you wanted to.



