J
John
I'm making a basic Template class, which is just a string that has
methods like replace {placeholder} with value. I would like this class
to have the ability to do things like
t = Template.new('lalala')
t.gsub!('a','o')
Right now I can do that by doing
def gsub!(match, replacewith)
@s = @s.gsub!(match, replacewith)
end
but this seems clunky. If I make Template a subclass of String, I
could just use gsub directly.
My question is, how to do this? Where does a String instance keep it's
data? Right now my Template class keeps its string in an instance
variable called @s. But doing
mystring = "attempt"
mystring.instance_variables
results in [].
I hope my question is clear. When I write my own classes, I can keep
data in instance variables. But if I subclass a built-in class like
String, I don't know where the data is.
methods like replace {placeholder} with value. I would like this class
to have the ability to do things like
t = Template.new('lalala')
t.gsub!('a','o')
Right now I can do that by doing
def gsub!(match, replacewith)
@s = @s.gsub!(match, replacewith)
end
but this seems clunky. If I make Template a subclass of String, I
could just use gsub directly.
My question is, how to do this? Where does a String instance keep it's
data? Right now my Template class keeps its string in an instance
variable called @s. But doing
mystring = "attempt"
mystring.instance_variables
results in [].
I hope my question is clear. When I write my own classes, I can keep
data in instance variables. But if I subclass a built-in class like
String, I don't know where the data is.