Ruby Lib

Hackety Hack will include some documentation for common Ruby methods. With the hope of shortening the docs and simplifying the examples.

Modules: Comparable, Enumerable, IO

Special Types

Ruby has three special types: nil, true and false.

nil

Nil is one of a kind. Literally, it means "nothing". Nil is a good thing to put in a variable if you really want that variable to be blank.

Lots of Ruby methods return nil. Most of the time it means "nothing was found" or "nope, that does nothing." It's a safe way of giving back nothing rather than tossing an error.

 #!ruby
 list = [1, 2, "January", [3, 4, 5]]
 found = list.detect { |x| x == "February" }
  #=> nil

In this example, we're searching the list for the string "February". Nil comes back. A prime example of "nothing was found."

You can check for nil by using if.

 #!ruby
 if found
   puts "Found February!"
 end

Nil has a handful of methods! Because: everything in Ruby is an object. This means everything is a useful thing which has its own actions (called methods.)

One method, nil? is on every object. But it only says true on the one and only actual nil.

 #!ruby
 if found.nil?
   puts "The `found` variable is really nil!"
 end

true and false

The true and false types are yin and yang, yes and no, positive and negative.

Array

An Array is a list of things. Especially useful when you have some things you want to keep in a specific order.

Create a new Array by using square brackets to surround a list of items. Remember it as a caterpillar which is stapled into your code. The square brackets are the staples at each end. The commas between each items are the legs of the caterpillar.

 #!ruby
 soap_brands = ["Zest", "Irish Spring", "Dove"]

Arrays can be easily added to with the push method. Or the << operator, which is a symbol meaning "push".

 #!ruby
 soap_brands.push "Lava"
 soap_brands << "Ivory"

Arrays start their counting at zero. This means the first item in the list is at position zero.

 #!ruby
 feed = Web.fetch("http://redhanded.hobix.com/index.xml")
 feed.items = [feed.items[0], feed.items[1], feed.items[3]]

The above code makes a new Array from the first, second, and fourth items in the feed.

Oh, one other thing. You can also use negative positions, which count from the end of the list. So, -1 is the position of the last item. And -2 is the second to last item and so on.

 #!ruby
 puts soap_brand[-1]

at( index: a Number )

Gets an item at a certain position. Arrays start at zero, so at(0) will give you the first item from the Array.

 #!ruby
 huxtables = ["Heathcliff", "Clair", "Denise", "Theo", 
              "Vanessa", "Rudy"]
 a.at(0)     #=> "Heathcliff"
 a.at(-1)    #=> "Rudy"

As shown in the last line, negative positions can be used.

clear()

Empties the array. Supposedly, so you can start filling it again.

 #!ruby
 list = File.readlines('README.txt')
 list.clear    #=> []

collect() { |item| ... }

A block must be attached to the collect method. The block loops through the Array and hands each item into the block. Whatever the block hands back is placed back into a new array at that same position. This is a great way to edit each item in an Array!

 #!ruby
 dogs = ["Pluto", "Astro", "Benji", "Lassie"]
 updogs = dogs.collect { |item| item.upcase }
 updogs
  #=> ["PLUTO", "ASTRO", "BENJI", "LASSIE"]
 dogs
  #=> ["Pluto", "Astro", "Benji", "Lassie"]

So, it doesn't change the first Array. See? It just makes a new Array, based on the old one.

If you want to make a slightly different copy of an Array, collect (also called map) is a great place to start.

collect!() { |item| ... }

Just like collect above: the block handles every item in the Array and changes the item. But instead of making a new Array, it changes the Array itself. Which is why this method has an exclamation mark. It's dangerous: it'll replace everything in the Array!

 #!ruby
 dogs = ["Pluto", "Astro", "Benji", "Lassie"]
 dogs.collect! { |item| item.length }
 dogs
  #=> [5, 5, 5, 6]

See, the Array was overwritten. With the length of each item. And, in a dangerous way: all the dog's names... are gone.

compact()

Removes all the nils from the Array. In case you want only things of substance. I mean what is nil good for anyway?

 #!ruby
 ["a", nil, "b", nil, "c", nil].compact
  #=> ["a", "b", "c"]

concat( an Array )

Adds an Array to the end.

 #!ruby
 ["ducks", "mule"].concat( ["sheep", "cats"] )
  #=> ["ducks", "mule", "sheep", "cats"]

Incidentally, you can also use += to do the same thing.

 #!ruby
 animals = ["ducks", "mule"]
 animals += ["sheep", "cats"]

map { |item| ... }

An alias for collect, which was described earlier on this page.

map! { |item| ... }

An alias for collect!, check it out, it's up higher on this page.

Dir

Dir is short for "directory." Which means: a folder on your hard drive. Use Dir to list files inside folders.

File

A File is a file on your hard drive. Like a text file or an email or an MP3.

Fixnum

A Fixnum is a number. 1, 2 and 3. And so on!

Hash

A Hash is a dictionary of pairs.

Object

An Object is just a blank thing. You will probably never use Object on its own.

But everything -- numbers, strings, arrays, etc. -- is an object. So all of these methods can be used on any object! Thus this page.

Range

A Range is a set of numbers. Like 1 through 10 is a Range.

One through ten looks like this: (1..10)

Regexp

Regexp is short for "regular expression". A Regexp is for finding patterns in words and paragraphs. It's not for the faint of heart.

String

A String is a series of letters. When you load a file, you pull a big long String out of it. A Web page is really just a String. Anything containing words, sentences, paragraphs is a String.

Even pictures are Strings! (Unreadable, messy-looking Strings.)

Symbol

A Symbol is a very, very simple kind of String. Symbols make the code look a bit prettier. Rather than having to quote small words, just stick a colon in front of the word an you have a :symbol!

Time

A Time object contains a specific date and time, right down to the milliseconds. And with time zone information.

You can always get the current time with Time.now!

URI

A URI is a web address.

Advanced: Class

Advanced: Exception

Advanced: Marshal

Advanced: Module

Advanced: Proc