T-16 to HW Due

Today I started with Ruby in 100 minutes being a Ruby newbie and installed all the things they recommended for a Mac OSX Mountain Lion system. My only hicup was with RVM which complained: mkdir: /myusername.rvm/src: Not a directory so, following a tip I found I just made the directory .rvm inside my username’s home directory (why isn’t there a slash between the myusername and the .rvm in the error message?).

Since all this installation took a while (Xcode is a 1.6 gig download, and RVM took about 15 min to install), I started looking at a git tutorial since I know nothing about it, and I figure I’ll want to be using git and github soon with all my projects.

Some notes on Git

Git by itself is a version control system, distributed in the top directories of all your projects, instead of having its own centralized storage directory.”Staging” with the git add command notifies git about the changes you’ve made to your project, without recording these changes in the repository; files need to be “added” to be “committed,” since only tracked files are committed, this allows for fine tuning of what you commit.

And so far, I stopped just before lab 8.

Notes on Ruby in 100 minutes

While on part 4, I discovered that it seems as if double quotes are different than single quotes for string interpolation, meaning that:
def make_toast(color)
  puts 'du it #{color}'
end

returns du it #{color} no matter what argument is passed to the method. The intended effect from running the method with the argument 'burnt', being du it burnt can only be accomplished with double quotes:
def make_toast(color)
  puts "du it #{color}"
end

Also, sort of annoyingly, it looks as if each block, whether it be a class, function, or loop, needs to end with end; practical, but annoying if transitioning from python.

At the end of a block of a method in Ruby, some value will automatically always be returned, by default this is the last expression that is evaluated. If we can return the object that we used the method on (with return self), then methods can be done back to back, called method chaining. So for instance, with:

class PersonalChef
  def make_toast(color)
    puts "du it #{color}"
  end
  def make_milk(animal)
    if animal == 'cow':
      puts 'moo!'
      return self
    end
  end
end

The command frank.make_milk('cow').make_toast('brown') will work fine, but frank.make_toast('brown').make_milk('cow') will not, since the make_toast method doesn’t contain a return self.

And I got distracted before I could continue, stopped at part 5 but feeling great so far!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s