Seven languages in 7 week (1)

Day 2: Floating Down from the Sky
At the time, one of the most striking scenes in Mary Poppins was her entrance. She floated into town on her umbrella. My kids will never understand why that entrance was groundbreaking stuff. Today, you’re going to experience a little bit of the magic that makes Ruby click. You’ll learn to use the basic building blocks of objects, collections, and classes. You’ll also learn the basics of the code block. Open up your mind to a little magic.
Defining Functions
Unlike Java and C#, you don’t have to build a whole class to define a function. You can define a function right in the console:

>> def tell_the_truth

>> true

>> end
Every function returns something. If you do not specify an explicit return, the function will return the value of the last expression that’s processed before exiting. Like everything else, this function is an object. Later, we’ll work on strategies to pass functions as parameters to other functions.
Arrays
Arrays are Ruby’s workhorse ordered collection. Ruby 1.9 introduces ordered hashes too, but in general, arrays are Ruby’s primary ordered collection. Take a look:

>> animals = [‘lions’, ‘tigers’, ‘bears’]

=> [“lions”, “tigers”, “bears”]

>> puts animals

Lions

Tigers

Bears

=> nil

>> animals[0]

=> “lions”

>> animals[2]

=> “bears”

>> animals[10]

=> nil

>> animals[-1]

=> “bears”

>> animals[-2]

=> “tigers”

>> animals[0..1]

=> [‘lions’, ‘tigers’]

>> (0..1).class

=> Range
You can see that Ruby collections will give you some freedom.

If you access any undefined array element, Ruby will simply return nil. You will also find some features that don’t make arrays more powerful but just make them easier to use. animals[-1] returned the first element from the end, animals[-2] returned the second, and so on. These features are called syntactic sugar, an added feature for convenience. The animals[0..1] expression might look like syntactic sugar, but it’s not. 0..1 is actually a Range, meaning all numbers from 0 to 1, inclusive.
Arrays can hold other types as well:

>> a[0] = 0

NameError: undefined local variable or method ✎a’ for main:Object

From (irb):23

>> a = []

=> []
Oops. I tried to use an array before it was an array. That error gives you a clue to the way Ruby arrays and hashes work. [] is actually a method on Array:

>> [1].class

=> Array

>> [1].methods. include?(‘[]’)

=> true

>> # use [1].methods. include?(:[]) on ruby 1.9
So, [] and []= are just syntactic sugar to allow access to an array. To do this right, I need to put an empty array into it first, and then I can play around with it some:

>> a[0] = ‘zero’

=> “zero”

>> a[1] = 1

=> 1

>> a[2] = [‘two’, ‘things’]

=> [“two”, “things”]

>> a

=> [“zero”, 1, [“two”, “things”]]
Arrays don’t need to be homogeneous.

>> a = [[1, 2, 3], [10, 20, 30], [40, 50, 60]]

=> [[1, 2, 3], [10, 20, 30], [40, 50, 60]]

>> a[0][0]

=> 1

>> a[1][2]

=> 30
And multidimensional arrays are just arrays of arrays.

>> a = [1]

=> [1]

>> a. push(1)

=> [1, 1]

>> a = [1]

=> [1]

>> a. push(2)

=> [1, 2]

>> a. pop

=> 2

>> a. pop

=> 1
Arrays have an incredibly rich API. You can use an array as a queue, a linked list, a stack, or a set. Now, let’s take a look at the other major collection in Ruby, the hash.
Hashes


1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)



Seven languages in 7 week (1)