Hi,
What does everyone thing of this as an RCR to change the behavior of
the String class?
Basically, When I say
str = "Joe"
puts str[0]
I want it to print "J" not the ascii code for "J". I think the need
to get the ascii code is much rarer then getting the actual character.
much rarer. It seems pragmatic to change it. Otherwise you have to
say:
puts str[0..0]
which is weird.
The other is, if I say:
s[2] = "seph likes ruby a lot"
It should print out:
Joseph likes ruby a lot.
Why should the programmer have to worry about getting the new strings
length?
In the spirit of Guy Decoux, I have a b.rb to sort of explain what I
am proposing:
joe@merry:~$ cat b.rb
#!/usr/local/bin/ruby
class BetterString < String
def [] (*args)
if (args.length == 1 and args[0].kind_of?Fixnum)
super(args[0]..args[0])
else
super(*args)
end
end
def []= (*args)
if (args.length==2 and args[0].kind_of?Fixnum and args[1].kind_of?String)
super(args[0], args[0]+args[1].length, args[1])
else
super(*args)
end
end
end
s = BetterString.new("please matz")
puts s[0]
puts s[0..5]
s[7] = "matz the great"
puts s
joe@merry:~$ ./b.rb
p
please
please matz the great
thanks,
-joe