I have been trying to get the hang of metaprogramming in ruby by reading other people’s code on the same. I have this piece of code that is probably too cryptic for me to debug. Here is the code.
#!/usr/bin/env ruby
class Person
def print_name
p "#{Person.identity.name}"
end
class << self
attr_accessor :identity
def identify()
self.identity ||= Identity.new
yield(identity)
end
end
class Identity
attr_accessor :name
def initialize
@name = "kibet"
end
end
end
me = Person.new
me.print_name
And the error I’m getting is this
`print_name': undefined method `name' for nil:NilClass (NoMethodError)
from ./meta_config.rb:28
Help’s highly appreciated.
I vaguely understood what you were trying to do there.. Check this out
Things to note: