When I needed to convert from the built-in String
class to a custom class called MyString
, I did it via the following:
class MyString < String
#Class body here
end
class String
def to_MyS
MyString.new self
end
end
foo = "bar"
puts foo.class #=> String
foo = foo.to_MyS
puts foo.class #=> MyString