The best kittens, technology, and video games blog in the world.

Monday, July 17, 2006

new kind of hacking

There are two kinds of objects, those with identity and those without. Objects without identity (value objects) are considered equivalent if they have the same fields, are usually used for boring stuff, and are usually immutable. On the other hand two objects with identity are considered different even if they have exactly the same state at the moment - they are usually mutable and interesting. It's kinda intuitive that a and b are the same thing, but c and d are not:

a = Complex.new(1.0, 2.0)
b = Complex.new(1.0, 2.0)
c = Array.new()
d = Array.new()
Now what if we have some value objects and we want to attach state to them ? For example we want to mark some complex numbers as lucky while others as unlucky ? Doing it naively wouldn't work: a = Complex.new(1.0, 2.0) a.lucky = true b = Complex.new(1.0, 2.0) print b.lucky # -> false And it's not obvious how to do that without introducing huge changes in the program. Well in Java at least. Now in Ruby on the other hand, because new is actually a normal method and not a magical constructor, we need just 3 lines of code...
class ValueClass
  def self.new(*args)
    @world ||= {}
    @world[args] = super(*args) unless @world.include? args
    @world[args]
  end
end

class Person < ValueClass
  attr_reader :first_name, :last_name
  attr_accessor :sucks
  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
  end

  def to_s
    "#{first_name} #{last_name} #{sucks ? 'sucks' : 'rocks'}\n"
  end
end

a = Person.new("Weasley", "Crusher")
a.sucks = true

b = Person.new("Weasley", "Crusher")
print b # -> Weasley Crusher sucks
and it corrently states that Weasley Crusher sucks. Even works with subclassing:
class Dog < Person
  def to_s
    "Dog #{first_name} #{last_name} #{sucks ? 'sucks' : 'rocks'}\n"
  end
end

a = Person.new("Bill", "")
a.sucks = true
p a # -> Bill sucks

b = Dog.new("Bill", "")
p b # -> Dog Bill rocks

2 comments:

Anonymous said...

Just wanted to say, that is one damn cute picture there. The bone and the tongue sticking out, almost a perfect photo if not for the somewhat akward angle. Anyhoo...*sneaks in question abour jrpg* Taking a break from jrpg or are you just too busy to work on/talk about it much lately?

taw said...

Cute Overload! ;) is a great source of such photos :-)

Oh yeah, jrpg, well, basically the next thing I need to do with jrpg is to debug font problems under Windows. And you can imagine it's not the most fun thing to do, so I'm kinda procastinating on jrpg :-)