Random Code 5: opoo

:: CodeCritic, Programming Languages

By: John Clements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  def link
    if f.linked_keg.directory? and f.linked_keg.realpath == f.prefix
      opoo "This keg was marked linked already, continuing anyway"
      # otherwise Keg.link will bail
      f.linked_keg.unlink
    end

    keg = Keg.new(f.prefix)
    keg.link
  rescue Exception => e
    onoe "The linking step did not complete successfully"
    puts "The formula built, but is not symlinked into #{HOMEBREW_PREFIX}"
    puts "You can try again using `brew link #{f.name}'"
    keg.unlink

    ohai e, e.backtrace if ARGV.debug?
    @show_summary_heading = true
  end

  def fix_install_names
    Keg.new(f.prefix).fix_install_names
  rescue Exception => e
    onoe "Failed to fix install names"
    puts "The formula built, but you may encounter issues using it or linking other"
    puts "formula against it."
    ohai e, e.backtrace if ARGV.debug?
    @show_summary_heading = true
  end

  def clean
    require 'cleaner'
    Cleaner.new f
  rescue Exception => e
    opoo "The cleaning step did not complete successfully"
    puts "Still, the installation was successful, so we will link it into your prefix"
    ohai e, e.backtrace if ARGV.debug?
    @show_summary_heading = true
  end

  def pour
    fetched, downloader = f.fetch
    f.verify_download_integrity fetched, f.bottle_sha1, "SHA1"
    HOMEBREW_CELLAR.cd do
      downloader.stage
    end
  end

Apparently, this is Ruby. This is taken from the most-forked Ruby project on GitHub, “homebrew”—a package manager for OS X. As usual, this piece of code is selected randomly from the project’s source code.

I honestly haven’t looked at a lot of Ruby, but there are a number of interesting things about it.

First of all, I’m guessing “opoo” and “onoe” aren’t built-in forms. Despite the silliness, though, the names were clearly both chosen to be four characters long, so that the code lines up nicely.

Also, it appears that Ruby has a fairly declarative style; I’m not seeing a sequence of actions … well, maybe I am. Perhaps I’m basing that on other pieces of Ruby I’ve read.

It looks like the syntactic designers of Ruby were extremely focused on eliminating delimiters; no curly braces, no parens for applications, no semicolons at the end of lines. I also appreciate the nifty unquote form that’s possible with hash-curly in strings. Also, it looks like there’s some nice pattern-matching on local variable definitions.

I definitely like the nice short functions; I have no idea if that’s characteristic of the language as a whole.

I’m also intrigued by the ‘require’ form that occurs inside of the definition of ‘clean’; does Ruby have local imports?

Finally, I would point out that this code doesn’t seem to have any comments, but that on the other hand it really doesn’t need them all that much. I would have appreciated a purpose statement for each function, but I can deal with not having them.