Picking up the axe

posted Mon, 29 Aug 2005 00:42:00 GMT by Jonas Bengtsson

It’s been a while since I’ve been coding Ruby. I learned the basics back in 2002 but since I’m much more fluent in Python I very rarely use Ruby when I need something done. But this time I have the motivation of Rails and a newly bought Programming Ruby 2nd edition (a.k.a. PickAxe).

Now to some random incoherent things I’ve learned about Ruby recently.

The most important and interesting aspect of the language, IMHO, is blocks, and I think I grok it now and am able to know when to apply them.

There were two conventions that made Rails-code hard for me to understand before: symbols and e.g. belongs_to. Kevin Clark taught me symbols and this piece of code describes how belongs_to et al. work:

def tinker_with_class
  class_eval do
    def hello
      puts "Hello world!"
    end
  end
end

class Hello
  tinker_with_class
end

h = Hello.new
h.hello

Output: Hello world!

One thing that I wasn’t aware of before was that Range supports custom classes (implementing the succ and <=> methods), which is neat and something that Python lacks.

Mixins seems to be a really good means of reuse, but I don’t know that much about them for a couple of chapters.

Private methods and variables can only be accessed by the own object, i.e. you can’t access private methods and variables of objects of the same class (as you can in Python, C++, Java and most other languages).

Many of the Perlisms seems to be less preferred now than the first edition of the PickAxe, which is a good thing. Who wants to remember the difference between $!, $&, $., $_, $~ etc?

The main gripe I have is that irb doesn’t work that well with Swedish keyboard layout on Windows, which is a shame since interactive Python was instrumental in my learning of Python.

Anyhow, I like most aspects of Ruby and I hope it will become as natural to me as Python.

Comments Zero comments