published on 31.01.2006 23:25.

completely useless ruby code

this chunk of code is completely useless, apart from the fact that it let me practise how ruby does arrays, objects, constants, observers and singletons. and how to pass a variable number of arguments to a function method and set defaults for arguments that weren’t passed. plus it had me thinking about “what-goes-where” and coupling in object oriented programming.

maybe not so useless after all!

#
# for documentation see the UML diagram at the following url
# @url <a href="http://www.flickr.com/photos/calicojane/93113537">http://www.flickr.com/photos/calicojane/93113537</a>
#
require 'observer'
require 'singleton'

class StickFigure
   MALE = 0
   FEMALE = 1   
   attr_reader :name
   def initialize(attrs)
      @name     = attrs[:name] ||= 'Anonymous Coward'
      @sex      = attrs[:sex] ||= MALE
      @is_lazy  = attrs[:is_lazy] ||= false
      @in_peril = false
   end
   def in_peril!
      @in_peril = true
      puts @name + ' is in peril!'
   end
   def to_s
      puts "\nHi! I'm " + @name + ' and I am ' + (@is_lazy == true ? 'a little' : 'not') + ' lazy.'
   end   
   alias :introduce_self :to_s   
end

class CoffeeDrinkingStickFigure < StickFigure
   def have_coffee
      puts @name + ' has a cup of coffee.'
      pot = get_coffee_pot
      pot.pour_coffee
      if pot.needs_refill?
         make_more_coffee unless @is_lazy
      end
      return_to_desk
   end

   #
   # :TODO: the stickfigure should NOT be responsible for notifying it's observer ... better solution?
   # this part is not so pretty.
   # 
   def return_to_desk
      puts @name + ' returns to ' + (@sex == FEMALE ? 'her' : 'his') + ' desk.'
      get_coffee_pot.notify_observers(get_coffee_pot, self)
   end   
   def make_more_coffee
      puts @name + ' refills the coffee pot.'
      pot = get_coffee_pot
      pot.refill
   end   
   private
   def get_coffee_pot
      CoffeePot.instance
   end   
end

class CoffeePot
   include Singleton
   include Observable
   attr_reader :cups_left
   CUPS_WHEN_FULL = 2.5   
   def initialize
      @cups_left = 0
      self.refill
      self.add_observer(BigBrother.new)
   end   
   def pour_coffee
      @cups_left -= 1
      changed
   end   
   def refill
      @cups_left = CUPS_WHEN_FULL
      changed
   end
   def is_empty?
      @cups_left == 0
   end   
   def needs_refill?
      @cups_left < 1
   end   
end

class BigBrother
   def update(pot, who_got_coffee)
      if pot.needs_refill?
         puts 'The coffee pot needs a refill and ' + who_got_coffee.name + " didn't refill it!\n"
         who_got_coffee.in_peril!
      else 
         cup_string = pot.cups_left == 1 ? 'is 1 cup' : 'are ' + pot.cups_left.to_s + ' cups'
         puts "There #{cup_string} of coffee left."
      end
   end
end

stick_figures = [
   CoffeeDrinkingStickFigure.new(:name => 'Miss Piggy', :sex => StickFigure::FEMALE),
   CoffeeDrinkingStickFigure.new(:name => 'Bert'),
   CoffeeDrinkingStickFigure.new(:name => 'Kermit', :is_lazy => true),
   CoffeeDrinkingStickFigure.new(:name => 'Ernie', :is_lazy => true)
]

stick_figures.each do |stick_figure|
   stick_figure.introduce_self
   stick_figure.have_coffee
end

Posted in , ,  | Tags , ,

published on 30.01.2006 15:53.

why we love internet explorer so much

what’s wrong with this javascript?

function showDetailInNewWindow()
{
   // set new window properties
   top    = 200;
   left   = 250;
   width  = 680;
   height = 300;
   // ... proceed to open new window
}

the error message ie gave me: “not implemented, line 26, character 7”. in _which _of the included js files, you ask? well if it gave that information out, the error would be too easy to find, wouldn’t it?! and when you finally debugged which file it was, the line number was a couple of lines off.

this error message is as useful as asking your grandma’s parrot what was wrong with the code. except the parrot’s reply would be more intelligent.

2 hours lost. talk about efficient development. i’ll consider writing my front-ends in xul.

the error was (i figure): you shouldn’t use “top” inside a function. JS has all global vars in function scope as well, and ie allows you to omit “window” in some expressions. i.e. ie thought i was trying to say “window.top = 200”. which it

  • should have just done
  • complained that it’s a read-only property
  • complained that the object window doesn’t have that (public) property

or maybe top is a completely reserved keyword, why didn’t it tell me???

of course i only had to resort to opening the details in a new browser window since IE first needed loads of hacks and javascript expressions to get the data table i am displaying to look it as i wanted, and then, second completely messed up the while document when dynamically embedding the detail information into the current page (“ajax”).

ie 6 is definitely web 1.0.

Posted in , ,

published on 27.01.2006 16:17.

migration to typo trunk revision

… wasn’t too hard, thanks to the rake migrate command and typo’s nice upgrading system, which slash7 had pointed to.

conversion of all mysql tables to InnoDB didn’t work at first. since i’m still on mysql 4.0 the upgrade script 15 and the ruby on rails’ create_table method failed. they both use the ENGINE keyword for specifying the table type to use. in order to get things to work, i had to replace ENGINE with TYPE.

btw. typo claims that running off the latest trunk revision is no problem thanks to the extensive test coverage. however most of those tests had some failures on my box; well, let’s see what happens - typo seems to be running fine. it _is_ a little awkward that you have to keep tests up to date with the latest code changes; maybe they just forgot.

warning: before running tests, make sure you have configured different databases for test and production environments, otherwise you production database will be gone!

Posted in ,

published on 21.01.2006 13:48.

now with 100% more chunky bacon!

the foxes

switched to typo

today i migrated my site from a home-brewn php/one-html-page solution to the ruby on rails powered weblog typo (which is not to be confused with typo3). what had to be done:

  • locally update ruby on rails to version 1.0 and install typo.
  • parse the existing html page with all the log entries, and feed the extracted posts into the ruby on rails database.
  • find a nice template and tweak the stylesheet here and there.
  • fix all image links to be absolute, convert the log entries to utf-8 (had to install the iconv library for ruby).
  • upload the locally prepared installation to the webserver, create and fill the online database.
  • compile ruby, ruby-iconv, ruby-fcgi, ruby-mysql, pcre and lighttpd on the webserver; then install ruby on rails.
  • configure apache to forward (proxy) all requests to lighttpd, configure lighttpd to embed ruby via fcgi
  • write a start/stop-script for lighttpd and make sure lighttpd is run on startup.

all of this took about twelve hours to complete. more than half of the time was spent preparing the webserver — finding out what was needed to get ruby and lighttpd work together over fcgi.

if you’d like to see conversion scripts, take a look at the extended content of this post.

(i am) happy rubying!

try ruby if you like – an interactive ruby shell that runs directly your web browser.

you could also visit why’s hilarious blog, or his poignant guide to ruby, where you’ll find many more cartoon foxes!

Read more...

Posted in ,