Hi,

I was trying to find the equivalent to substr() in ruby,
and came across String#slice, which seemed to have that
and much more.

Well, it is missing something, unfortunately.

I will demonstrate:

Suppose $string="Hello there"

In Perl (and PHP),

substr($string, 4, 1) == "o"
and substr($string, 4, 3) == "o t"
but substr($string, 4) == "o there"

In other words, if you leave out the third parameter ('length'), 
it just goes to the end of the string. Makes perfect sense, and
is balanced well by the functionality to extract a single character.

In Ruby,

string.slice(4,1) == "o"
and string.slice(4,3) == "o t"
but string.slice(4) == 111  	# (!)

I would expect String#slice() to work the same way as Perl's substr()
when using Fixnum parameters...

What is worse, if I *do* wish to get a substring from a starting position
to the end, it seems I have to use an embedded function:

string.slice(4..string.size) == "o there"

or else use some other method:

string.unpack("x4a*")[0]  	# tricky


Perhaps this dilemma is due to the fact that String#slice is an 
alias to String#[] ?

string[4] == 111	# cf. [ruby-talk: 01528] 

Here it makes sense that a single Fixnum refers to a single character (ok, byte),
because it appears to be acting as an index, not a starting position.


So, now what?

Perhaps it could be allowed that a range need not have an ending value?

So then:

string.slice(4..) == "o there"

And also,

string[4..] == "o there"


Any other ideas/comments?


Guy N. Hurst


-- 
HurstLinks Web Development    http://www.hurstlinks.com/
Norfolk, VA - (757)623-9688
PHP/MySQL - Ruby/Perl - HTML/Javascript