06 Mar, 2011, Chris Bailey wrote in the 1st comment:
Votes: 0
module ModuleBase
def extended(obj)
instance_variables.each do |iv|
var_name = iv.to_s
var_value = instance_variable_get(var_name)
if obj.responds_to?(var_name)
if var_value.class == Integer
obj.instance_variable_set(var_name, (obj.instance_variable_get(var_name) + var_value))
end
else
obj.instance_variable_set(var_name, var_value)
end
end
end
end

module Gettable
include ModuleBase
def get
puts "You get the #{@name}"
end
end

module Droppable
include ModuleBase
def drop
puts "You drop the #{@name}"
end
end

module Swingable
include ModuleBase
def swing
puts "You swing the #{@name} and do #{@damage} damage."
end
end

module Sharp
include ModuleBase
@damage = 1
end


class Weapon
attr_accessor :name, :damage
def initialize(name, damage)
@name = name
@damage = damage
extend Swingable
extend Droppable
extend Gettable
end
end

class Sword < Weapon
def initialize(name, damage)
super
extend Sharp
end
end


Playing around with modules. The idea is that an object extended the Sharp module would have its damage incremented by 1. If the object doesnt contain the variable then it gets set on it. Make any sense? Wtf is wrong! =)
07 Mar, 2011, Runter wrote in the 2nd comment:
Votes: 0
That's because @damage is local to the object it's in. i.e. @damage in the context of defining a class is the instance variable for that class/module object, not the instances it creates. You need to call super and define initializers to get the desired functionality, but it's not going to be as clean as you thought.

There may be a callback on extend that works too *shrug*.
07 Mar, 2011, Chris Bailey wrote in the 3rd comment:
Votes: 0
Well. The whole concept is kind of strange, I was just toying with modules. I couldn't figure out what I was doing so I took another route.

class BaseItem
def initialize(name)
@name = name
end
def add_tags(*tags)
tags.each do |mod|
mod.instance_variables.each do |iv|
var_name = iv.to_s
var_value = mod.instance_variable_get(var_name)
if instance_variable_get(var_name).class == Fixnum
instance_variable_set(var_name, (instance_variable_get(var_name) + var_value))
else
instance_variable_set(var_name, var_value)
end
end
end
end
end


module Sharp
@damage = 1
end

module Heavy
@weight = 3
@damage = 2
end

module Flaming
@damage = 3
end


class Weapon < BaseItem
def initialize(name)
super name
@damage = 1
end
end


sword = Weapon.new('Blade of Stuff')
sword.add_tags Sharp, Heavy, Flaming
07 Mar, 2011, Runter wrote in the 4th comment:
Votes: 0
I dunno. I don't really like the design. It reminds me of migrations. You'd have to define the backwards and forwards to add or remove a property. I've toyed around with the very same thing in the past. It's much better to do this for the purpose of changing functionality rather than changing stats. Also you can calculate states based on what composes an object rather than a counter.

if obj === Sharp
meh
end
09 Mar, 2011, Chris Bailey wrote in the 5th comment:
Votes: 0
The idea definitely came from bud-light and migrations. :P
09 Mar, 2011, Runter wrote in the 6th comment:
Votes: 0
Chris Bailey said:
The idea definitely came from bud-light and migrations. :P


I hated migrations so much I quit using activerecord for datamapper in rails projects.
0.0/6