T-15 to HW Due

Finishing up Ruby in 100 minutes

Continuing with Ruby in 100 minutes, I learned about some string methods:

  • (Format of the following = Ruby is like Python):
  • thing.length is like len(thing), string.delete("letter in string") is like string.replace("letter in string", "")
  • string.gsub("letter in string", "new letter in string") is like string.replace("letter in string", "new letter in string") (gsub replaces all occurring characters, while sub only replaces the first (RubyMonk))
  • string.split("where to split") is like string.partition("where to split") except that in Python, the letter where the split occurs is maintained and becomes an element of the output tuple, and in Ruby it is erased. Splice notation in Ruby is "string"[0..-1] but is "string"[0:-1] in python.

Combining strings when printing is a little different in Ruby versus python, if a and b are strings:

>> puts a + b
=> nil
>> puts a, b
=> nil

And Python:
In [4]: print a, b
letter new

In [5]: print a+ b

One unfortunate difference is that Ruby can get confused when printing numbers and strings on the same line, so adding a .to_s to the end of a number or a variable that is a number when printing is necessary (an alternative could be using string interpolation).

Interpolation, as mentioned yesterday, is done with #{insert_me} rather than the percentage sign system in Python.

While doing the date/time exercise at the end of unit 5, I learned about the different methods with the ‘date’ module.

A weird thing was introduced…Symbols which are immutable strings, that can define variables… In Chapter 6 of Eloquent Ruby, Olsen summarizes that strings have largely two disparate purposes in our code: 1) hold data, perhaps an input to a method, and 2) represent things, like courses of action in a method, by being an argument passed to the method; for instance, if open_cans is a method of ours, in: group_of_cans.open_cans(:all) versus group_of_cans.open_cans(:first_half) the symbols are human readable representations for the method’s functionality, rather than just an input string that will be manipulated. Symbols have thus been optimized for this representative behavior with some unique traits:

  • there is only ever one instance: if a = :all and b = :all, then they refer to the same exact :all This means equality operations with symbols are lightning fast. Plus, every mention of a symbol is not creating a new object stored separately in memory (Cooper 72)
  • A symbol will never change, and isn’t editable, :all cannot be transformed into :al

Symbols can be turned into a string too, with the .to_s method; this is reversed with the .to_sym method. But, overall, use a string instead of a symbol if you might want to edit it in any way, print, or concatenate (well, this can be okay if the symbol points to something that will be concatenated) with it. Symbols are like literal constants who have no value, and whose name is the most important thing; making symbols most useful when they store a concept rather than a value (Cooper 73).

Moving on to section 8, I found Ruby’s array << 4 is equivalent to python’s array.append(4) and many other little syntax differences (the << method works just like the + method with strings, but will end up using less memory since it appends one string to another, while the + makes a new combined string (RubyMonk)). The last 3 sections were very scant, and introduced some obvious things, but also some note obvious things like the double pipes (| |). My interest is piqued, maybe I’ll return to some other tutorials, but before that, I’ll try RubyMonk.

Installing rvm properly

I also realized that I never checked what version of Ruby I had installed, running ruby -v at the Terminal led me to find out that I had an outdated version, and trying to update with rvm like Ruby in 100 minutes and many others recommend, led me to find out that just running the installation commands for rvm and homebrew were not enough.

I first ran home-brew doctor to find many errors. I then did sudo vim /etc/paths and changed the file to say:


Then, following a blog I installed a new version of git using brew install git even though I already had one installed with Xcode, and Step 6 and 7 in the blog also led me to update rvm, ruby, and rails easily. Next time I want to update ruby, I’ll follow this screencast. I still ended up ignoring the majority of errors from running home-brew doctor like: "config" scripts exist outside your system or Homebrew directories. and Enthought Python was found in your PATH. and Unbrewed dylibs were found in /usr/local/lib. and Unbrewed .pc files were found in /usr/local/lib/pkgconfig..

On RubyMonk

  • Chaining methods is actually pretty cool. For instance, it is possible to sort the information the methods method spits out via 1.methods.sort.
  • An interesting thing that RubyMonk introduced is thinking about + - = == != [] and the like as methods, crazy!
  • “A String literal created with single quotes does not support interpolation. The essential difference between using single or double quotes is that double quotes allow for escape sequences while single quotes do not. What you saw above is one such example. “\n” is interpreted as a new line and appears as a new line when rendered to the user, whereas ‘\n’ displays the actual escape sequence to the user.” (From Here)
  • “It is conventional in Ruby to have ‘?’ at the end of the method if that method returns only boolean values.” versus finding out the index, which is not a boolean, would be just "I am a Rubyist".index'R'
  • a double pipe like || just means “or” like && means “and”
  • unless x is equivalent to if !x
  • In Ruby, ? and : can be used to mean “then” and “else” respectively.
  • Loops in Ruby seem incredibly strange after Python, I’ll definitely be reading about them in Olsen’s book and maybe supplementing with Cooper’s book
  • map can be used with arrays to create a new transformed array (where each element is twice the value for example), select is for filtering elements to a new array (like those that are divisible by 2)

And now I’m calling it a day. I’m not sure how I feel about the touch and go nature of RubyMonk. They’ve made it very fun, but I feel very disconnected when learning from it, kind of assuming what does what and feeling only sparingly confident, not well grounded enough to stray from the path that they’ve laid out; plus I’m not sure I would remember anything they’ve said without writing it down. Before I continue beyond the 50% mark, I’ll be reading a bunch of Eloquent Ruby first.


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