Ph values

Stephan on testing, software (good and bad), Ruby and the world at large

Archive for the ‘Uncategorized’ Category

Ruby, Sequel and Trees

Posted by stephan on 28. July 2009

While working on some tree structure a couple of unit tests failed, when creating some form of summary of said tree structure. First of all here’s a condensed form of the code:

require 'sequel'

DB = Sequel.sqlite
DB.create_table :items do
  primary_key :id
  String :name
  String :foo, :default => 'NOT SET'
  Integer :item_id
end

class Item < Sequel::Model
  one_to_many :items
  def summary
    if items.empty?
      return [ { self.name => self.foo } ]
    else
      items.inject( [] ){ | r, sub_res | r << sub_res.summary }
    end
  end
end

r1 = Item.create :name => 'R1'
r2 = Item.create :name => 'R2'

r1.add_item r2
r2.foo = 'Ding'
#r2.save

[ r1, r2 ].each{ |i|
  puts "Item    : #{ i.id }"
  puts "Direct Foo: #{ i.foo.inspect }"
  puts "Summary   : #{ i.summary.inspect }"
  puts
}

Notice, that I’ve commented out saving r2. The output is:

Item    : 1
Direct Foo: "NOT SET"
Summary   : [[{"R2"=>"NOT SET"}]]

Item    : 2
Direct Foo: "Ding"
Summary   : [{"R2"=>"Ding"}]

What I didn’t expect – and why the unit tests failed – is the ‘NOT SET’ in the r1 summary.

One way to end up with what I originally expected is to save r2. Another way is to set r2.foo before adding r1 to r1:

r2.foo = 'Ding'
r1.add_item r2

How ever I’m still not sure whether this is intended behaviour and why it should behave like this. (If it is, I’d like to know why.)
What do you think?

Posted in Ruby, testing | Tagged: , , | Leave a Comment »

Pasting Code Is OK

Posted by stephan on 10. October 2008

While reading about the DRY principle (for “Don’t repeat yourself”) and the evil of copy-and-paste coding (again), I started thinking what to do instead. Actually, what to do is more or less obvious: Put the code into a place where its accessible to be reused — a method, may be in a new or existing module or class.

So, whenever I feel the ‘need’ to copy code, I now think about cutting it, creating a new method and calling that. Apart form avoiding duplication, the code is now testable immediately by calling the method (instead of getting the surrounding code exercised). And the methods using the code gets shorter.

In the end, it boils down to pasting code being perfectly OK, it’s the copying that causes the trouble.

Posted in Programming, method call, testing | Tagged: , | Leave a Comment »

Euruko 2008 – Favourite Quotes

Posted by stephan on 30. March 2008

My favourite quotes of the conference: “…the nasty snake language…” (Matz about Python) and “I don’t know how to use Ruby” (Koichi on Ruby). 

Posted in Uncategorized | Tagged: , , | 1 Comment »

Ruby and Rails in Munich, Germany

Posted by stephan on 20. November 2007

There’s a “Ruby and Rails” meeting in Munich on Monday 26th November, starting at 19:30. The location is the “Augustiner am Dom”.
In case you’re coming for a beer or two, some Bavarian food etc. please drop me an e-mail at phvalue ät mac point com, so we can arrange a reservation.

Posted in Uncategorized | Tagged: , , , | Leave a Comment »

EURUKO 2007 is over…

Posted by stephan on 13. November 2007

So the European Ruby Conference is over for 2007 — a big “Thank you!” to everyone involved: Organisers, the Universitiy of Vienna for the great lecture hall and network we were allowed to use, the Viennese who did a great job getting it done, the speakers — keynote or not — and of course all the attendees.

Euruko 2007 - lecture hall entry I was not very surprised to see that Testing, Quality and Beauty of code was a topic again, as was meta programming and really interesting new applications. For more on that including links to the available slides please see the Euruko 2007 Wiki.

If you like to stay connected and watch the photos we shot in Vienna sign up at Facebook and join the “Eurukobackweb” group. There we’ll also try to get “Euruko 2008″ organised. Up to know it’s all open: If you’d like to see it happen in your city or country: Join the group. If you prefer a more sunny season: Join the group. Should you think it should be more organised: Join the group. Or is you “only” like to get in touch with Ruby programmers: Join the group. :)

Hope to see as many of you as possible again next year. And a few more new Eurukoristas. It’s fun, interesting and the coffee was excellent (remember? We were in Vienna).

Posted in Uncategorized | Tagged: , , | Leave a Comment »

TextMate, Ruby & String Interpolation

Posted by stephan on 7. November 2007

While preparing some code example for the Euruko 2007 I noticed a somewhat strange behaviour of TextMate while editing Ruby code. With the Ruby bundle being active, I expect CMD-R (“Apple-R”) to execute the Ruby code a hand. Which it does, unless the text cursor is inside a string interpolation expression.

An example: The first screen shot shows the example code, the cursor being positioned outside the #{ … }.

cursor_outside_srint_interpol.png

After CMD-R is hit, the output is the expected Ruby output as shown below.

outside_result.png

However, if the cursor is inside the string interpolation expression as below

cursor_inside_print_interpol.png

the result of hitting CMD-R doesn’t meet my expectation:


inside_result.png

I wonder why this happens. Apparently the behaviour has to do the the scope the cursor is in. A hint about how to avoid this is greatly appreciated.

Posted in Uncategorized | Tagged: , , | 3 Comments »

Pre Conference Dinner for Euruko 2007

Posted by stephan on 4. November 2007

The pre conference dinner for the European Ruby Conference 2007 will be on Friday the 9th November 2007 at 20:00.
The location is called Das Lederer.

The address is: A-1080 Wien, Skodagasse 28 / Ecke Lederergasse

Posted in Uncategorized | Tagged: , | Leave a Comment »

Ruby hits the UK as No. 1

Posted by stephan on 26. February 2007

So Ruby is the #1 in the UK :-)

The Official UK Singles Chart : 25.02.2007:
BBC Radio 1 UK charts

Also available for iPod, BTW.

Posted in Uncategorized | Leave a Comment »

A geek test…

Posted by stephan on 23. July 2006

Well, I took it and scored embarrassingly high. :–)


My computer geek score is greater than 100% of all people in the world! How do you compare? Click here to find out!

Anyway it is fun.

Posted in Uncategorized | Leave a Comment »