Tuesday, November 28, 2006
Ruby: Protected class methods
Given the following class subjects_xml and subjects_html are public, not protected.
It is possible to define protected class methods in various ways.
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
Comments:
<< Home
To use a visibility modifier? I'm not going to attempt that answer in a comment. =)
Hopefully someone has some information they can link to.
Hopefully someone has some information they can link to.
Really pedantic (sorry), but why:
class << 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.
class << 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.
Hey Jay,
You 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
Post a Comment
You 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
<< Home






