Enjoying my Vacation
February 14th, 2007 by MarkIt’s nice to be on vacation. Really nice. I had good times in Shanghai and Beijing last summer, and in Colorado the winter before, but staying at home and just relaxing isn’t too bad either. I’ve had time to see more of my friends, which is always good, and I’ve also had some time to do a little self studying, too.
The current subject of my study is Ruby. It’s a very interesting and very expressive programming language. I picked up Programming Ruby, 2nd edition, and I’ve been working through it. Some of Ruby’s language features are very familiar to me from Python or Perl. Others have been a bit more alien. Blocks and yield statements have been particularly hard for me. I can understand simple examples such as the following:
def fib_up_to(max)
i1, i2 = 1, 1
while i1 >= max
yield i1
i1, i2 = i2, i1+i2
end
end
The above function takes a number as it’s argument and a block to tell it what to do with the yield statment. The following call would print out a list of Fibonacci numbers up to 500:
fib_up_to(500) {|n| print n, " "}
Thus, 500 is passed to the function fib_up_to(), and it keeps looping while i1 is less than 500. The weird part for me is that each time the loop hits the yield keyword, it throws the value of i1 back to the {|n| print n, " "} block in the function call. The variable n is set to whatever i1 was, and then it’s printed out along with a space to make things look nice. Then the function loops again. The output would be all the Fibonacci numbers up to 500:
1 1 2 3 5 8 13 21 34 55 89 144 233 377
I can follow the example, but as somebody who has done most his coding in C, assembly, and Perl, this is a bit hard to wrap my mind around. I pass arguments to functions, but I’m not used to them passing variables back to a calling block. The concept feels alien and unnatural. I suppose that’s one of the good things about learning more programming languages, or human ones for that matter. It forces me to think differently.
:
February 14th, 2007 at 5:21 am
Awesome, Ruby is terrific. Its various structures were a bitch and a third to wrap my head around when I was learning it (and I am by no means a master) but once you get them down you can do some ludicrously powerful things in just a few lines of code, without resorting Perl-like syntax insanity.
February 14th, 2007 at 7:33 am
I hear you about the “thinking differently”—it was the Sapir-Whorf hypothesis that first got me started studying Chinese (”If I learn another language it will make me look at the world differently, and what can be more different from Chinese than English?”).
End result, unfortunately, is that I’m pretty fluent now, but haven’t noticed any change in thinking.
Learning programming languages, though… that does twist your mind right well! I grew up on c++/java (now work mostly in python), but wrapping my mind around lisp and haskell significantly changed the way I look at the world…
February 14th, 2007 at 7:36 am
I’m enjoying it too, John. Would you happen to have any suggestions for where I could find some good sample exercises to work through? That’s one thing the “Pickaxe” book seems to be missing. I started typing in the examples and messing around with them via the IRB, but now that I’m a 100 or so pages into the book it isn’t working so well.
February 18th, 2007 at 2:15 am
Nick said:
Nick, believe it or not, that’s half of my motivation to learn languages, too! Congrats on the job at Google. Have you ever had any opportunities to use LISP or Haskell on the job?
March 2nd, 2007 at 5:34 am
I’m sure I could find a few side projects that use LISP and Haskell, but I haven’t really looked for them—most everything here is written in C++/python/java.
I’ve typically only dived into new languages when I needed some intellectual stimulation to shake things up, and believe me since I’ve started at the Google I haven’t found any need for extra intellectual stimulation =)