ArrayMine inherits from Array and you can initialize Ruby arrays like that.
>> rooms = Array[3,4,6]
=> [3, 4, 6]
is the same as
>> rooms = [3,4,6]
=> [3, 4, 6]
Also the quite strange looking def join( sep = $,, format = "%s" )
is using the pre-defined variable $,
that is the output field separator for the print and Array#join
.
It could also have been done like this
rooms=["b",52,"s"]
print "We have " + rooms.map{|s| s.to_s+" bed"}.join(", ") + " rooms available."
The reason you can't do what you are trying to do with String
is because assignment is not a class method but [] on Array
is. Just new
it instead.
>> s = Substring.new("abcd")
=> "abcd"
>> s.checking_next
=> "abce"
You can't override assignment in Ruby, it's only so that setter methods looks like assignment but they are actually method calls and as such they can be overridden.
If you are feeling like being tricky and still want a similar behaviour as a=SubArray[1,2,3]
you could create a <<
class method something like this:
class Substring < String
def next_next()
self.next().next()
end
def self.<<(val)
self.new(val)
end
end
>> sub = Substring<<"abcb"
=> "abcb"
>> sub.next_next
=> "abcd"
>> sub<<" the good old <<"
=> "abcb the good old <<"
>> sub.class<<"this is a new string"
=> "this is a new string"