Previously on #ruby somebody told me that I do not need to use @name and self.name unless the variable name is already defined locally, which I was happy to learn would make my code a bit neater I thought. After playing around and adjusting a few of my classes I noticed that I could not access name (it is nil) after setting @name in initialize in the subclass.
The following works:
module EnvyGeeks
class Attributes
attr_accessor :persons_name
def initialize(name)
@persons_name = name
end
end
end
module EnvyGeeks
class MyAttributes < Attributes
def initialize(name)
@persons_name = name
puts persons_name
end
end
end
envygeeks = EnvyGeeks::MyAttributes.new("Jordon")
The following fails:
require "pp"
module Jekyll
class MyPages < Page
def initialize(site, base, page)
@name = page.split("/")
pp name # => nil
end
end
end
That class taps off this super class:
https://github.com/mojombo/jekyll/blob/master/lib/jekyll/page.rb
I’m a bit confused why name would not be working in this instance but in the first it does?
More information:
From: ./pages.rb @ line 19 in Jekyll::MyPages#initialize:
14: def initialize(site, base, page)
15: @site = site
16: @dir = "/"
17: @base = base
18:
=> 19: binding.pry
20: @name = page.split("/")
21: if name.length > 1
22: name.pop if name.last =~ /index.html$/
23: name = name.join("/") if name.length > 1
pry(#<Jekyll:Page @name=nil>)> @name = page.split("/") => ["index.html"]
pry(#<Jekyll:Page @name=["index.html"]>)> name => nil
Which version of Ruby is this? On 1.9.2 it works fine.