Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Created November 1, 2013 15:45
Show Gist options
  • Select an option

  • Save RoxasShadow/7267350 to your computer and use it in GitHub Desktop.

Select an option

Save RoxasShadow/7267350 to your computer and use it in GitHub Desktop.
class Pokemon
def initialize(&block)
@var = []
instance_eval &block
end
def method_missing(m, *args, &block)
if args.any?
args = args.first if args.length == 1
@var << { m => args }
instance_variable_set "@#{m}", args
else
return instance_variable_get "@#{m}"
end
end
def to_s
''.tap { |s|
#len_max = 1
#@var.each { |h|
# curr_max = h.keys.max.length
# len_max = curr_max if curr_max > len_max
#}
@var.each { |h|
s << "%-5s: %s\n" % [ h.keys.first, h.values.first ]
}
}
end
end
puts [].tap { |pokedex|
pokedex << Pokemon.new do
id 1
name :Bulbasaur
end
pokedex << Pokemon.new do
id 2
name :Ivysaur
end
}.join "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment