class BlogThe
def self.available_subjects(format)
case format
when :xml then subjects_xml
when :html then subjects_html
end
end
protected
def self.subjects_xml
[xml implementation]
end
def self.subjects_html
[html implementation]
end
end
def self.
code can be thought of as defining a class method of Blog, but it is actually defining a method on self, where self is the virtual class of Blog. The protected visibility modifier is being used to specify the visibility of the methods of Blog. Since the subjects_xml and subjects_html methods are being defined on self (the virtual class of Blog) the protected visibility is not applied.It is possible to define protected class methods in various ways.
class Blog
class << self
def available_subjects(format)
case format
when :xml then subjects_xml
when :html then subjects_html
end
end
protected
def subjects_xml
[xml implementation]
end
def subjects_html
[html implementation]
end
end
end
class Blog
def self.available_subjects(format)
case format
when :xml then subjects_xml
when :html then subjects_html
end
end
def self.subjects_xml
[xml implementation]
end
def self.subjects_html
[html implementation]
end
class<<self;self;end.send :protected, :subjects_xml, :subjects_html
end
What's a compelling reason to do this?
ReplyDeleteTo use a visibility modifier? I'm not going to attempt that answer in a comment. =)
ReplyDeleteHopefully someone has some information they can link to.
Really pedantic (sorry), but why:
ReplyDeleteclass << self;self;end.send :protected, :subjects_xml, :subjects_html
rather than:
class << self
protected :subjects_xml, :subjects_html
end
The second one just looks a little cleaner to me.
There are a few ways to do it, I just threw two examples up there...
ReplyDeleteHey Jay,
ReplyDeleteYou do know that because of the way Ruby inheritance is structured that protected doesn't really make sense for class methods.
As matz explains here