2011-02-06

Hello Ruby

Recently, I've been looking into the Ruby Programming Language and like most programmers I started by writing a "Hello World" program. Now let me say that I am not a fan of most Hello_World programs simply because I don't think they really give much information to the viewer of the code, so I thought I should share the code that I wrote.

There were a few programming language features that I thought should be included in my code.

An Object Oriented class

Quite a bit of my coding is Object Oriented and since most anything I program in the future using Ruby will be Object Oriented, this was a must.

A class that extends the functionality of the previous class

Writing my own classes is good and dandy, but very often I need to extend a base class from and included code library with additional functionality and I wanted to see what sort of coding requirements Ruby has for extending classes.

some sort of array thingy

Each programming language has various names for ways to store a collection of data: list, dictionary, hash table, array, and a few other things that I usually forget because I don't pay much attention to nomenclature. Anyway, I always end up having to program some way to deal with a list of words or numbers or something, so I made sure to include an array in my program.

Enter the Ruby

#!/usr/bin/env ruby

#define a class to print out some words
class PrintThing
  def initialize
    #we need a variable to hold the words to be printed
    @words = [] #use an empty array
  end
  
  #define a function to set the @words
  def set_words(words)
    @words = [words]
  end
  
  #define a function to output all of the elements of the words array 
  def print_words()
    puts @words.join" " )
  end
end

#extend the printthing class to be more advanced
class AdvancedPrintThing PrintThing
  #define a function to add an element to the words array
  def add_wordword )
    @words.pushword )
  end
  
  #define a function to truncate the words array
  def clear()
    @words = []
  end
end

if __FILE__==$0
  #create an instance of the PrintThing
  pt PrintThing.new
  #set some words
  pt.set_words"Hello World" )
  #print the words
  pt.print_words()
  #create an instance of the AdvancedPrintThing
  apt AdvancedPrintThing.new
  #set some words
  apt.set_words'Howdy Buddy!' )
  #print the words
  apt.print_words()
  #clear the word list
  apt.clear()
  #add a bunch of single words
  apt.add_word'How' )
  apt.add_word'are' )
  apt.add_word'you?' )
  #print the words
  apt.print_words()
end

What I learned

  • Defining a class is easy and a new class should have a constructor method named "initialize" if the class needs to do something when a new instance of the class is created
  • Extending a class can be done with the "<" symbol, so to code that class A extends class B is as simple as "class A < B".
  • a variables scope is determined by the variable's starting character.
    $ for global variables
    @ for instance variable
    @@ for class variables
    While this can look confusing at first, I like being able to determine at a glance exactly what the scope of a variable is.
  • There was no need to worry about code indentation. Ahhh relief.
  • Ruby does not create byte-code when an ruby program is run. sigh of disapproval

All things considered, I like the language and there is some rather impressive documentation available at http://www.ruby-doc.org/

Now quit reading, and go find yourself a programming project.

Comments
2011-04-10 senshikaze:
Have you checked out "why's poignant guide to ruby"? It is hilarious at least. chunky Bacon!

http://mislav.uniqpath.com/poignant-guide/book/chapter-1.html
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
4 plus 8
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008