Is there any easiest way to get the substring starting from the 3rd
character?
my solution:
str[3…-1] which seems quite odd :-/
Seems quite even to me
Seriously, the ability to negative-index strings and arrays like that
is a breath of fresh air compared to traditional compiled languages.
I see no need to improve on it.
What if, intuitive, we can write
str[3…]?
I rather doubt that’s going to happen. When you execute str[3…7],
the 3…7 isn’y arbitrary syntax, it’s a Range object. If Range
objects were allowed to be unbounded, then fine. So the question
really becomes, should Range objects allow unbounded ranges?
Any smarter solution?
Write a String#substring method if you like.
Cheers,
Gavin
···
On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
Is there any easiest way to get the substring starting from the 3rd
character?
my solution:
str[3…-1] which seems quite odd :-/
What if, intuitive, we can write
str[3…]?
Any smarter solution?
Besids ‘get used to -1’? Well, here’s some extension to ‘String’. I
did try my best to follow POLS. The intention is that left, right,
mid emulate BASIC’s left$, right$ and mid$ while head and tail
without any arguments have the results they should have from a
functional programming point of view.
class String
‘number’ leftmost chars
def left(number = 1)
self[0…number-1]
end
‘number’ rightmost chars
def right(number = 1)
self[-number…-1]
end
‘number’ chars starting at position ‘from’
def mid(from, number=1)
self[from…from+number]
end
“KONTRA Gergely” kgergely@mlabdial.hit.bme.hu schrieb im Newsbeitrag
news:20031114154849.GA12384@mlabdial.hit.bme.hu…
Hi!
Is there any easiest way to get the substring starting from the 3rd
character?
my solution:
str[3…-1] which seems quite odd :-/
What if, intuitive, we can write
str[3…]?
Any smarter solution?
Not as far as I know. You can do str[3,str.length-3] but this isn’t
really an improvement unless you value the absence of negative integers in
this expression.
Maybe some destructive versions of the above methods could be useful?
input=“diamonds are forever”
p input.right!(4) #-> “ever”
p input.left!(4) #-> “diam”
p input # → “onds are for”
···
On Sun, 16 Nov 2003 06:04:51 +0900, Josef ‘Jupp’ SCHUGT wrote:
class String
def left(number = 1)
self[0…number-1]
end
def right(number = 1)
self[-number…-1]
end
def mid(from, number=1)
self[from…from+number]
end
def head(position = 0)
self[0…position]
end
def tail(position = 0)
self[position+1…-1]
end
end
On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote:
Seriously, the ability to negative-index strings and arrays like that
is a breath of fresh air compared to traditional compiled languages.
I see no need to improve on it.
I do, but I have no good solution to the problem.
The problem being that positive indexes go from 0 to n-1, while negative
go from -1 to -n, which could introduce confusion.
But as I said, I know of no good solutions, since -0 is not a good way,
and making the first positive index a 1 is just as bad, for several reasons.
Useful… I have added your snippet at rubygarden (StringSub):
···
On Sun, 16 Nov 2003 06:04:51 +0900, Josef ‘Jupp’ SCHUGT wrote:
Besids ‘get used to -1’? Well, here’s some extension to ‘String’. I
did try my best to follow POLS. The intention is that left, right,
mid emulate BASIC’s left$, right$ and mid$ while head and tail
without any arguments have the results they should have from a
functional programming point of view.
This has to do with how Ruby views indices on arrays and array-like
structures (like strings).
[h] [e] [l] [l] [o]
0 1 2 3 4 5
The indices are “before” the actual element, so (5 … -1) represents
“end of the string to end of the string”, whereas (6 … -1)
reperesents (outside fo the string to the end of the string).
Some of this has to do with performance, I think, but it’s something
that makes sense, once you’re used to it. I think that it’s more
sensible than the typical implementations.
-austin
···
On Mon, 17 Nov 2003 11:40:13 +0900, Jon A. Lambert wrote:
I rather doubt that’s going to happen. When you execute str[3…7],
the 3…7 isn’y arbitrary syntax, it’s a Range object. If Range
objects were allowed to be unbounded, then fine. So the question
really becomes, should Range objects allow unbounded ranges?
“Linus Sellberg” linse428@student.liu.se schrieb im Newsbeitrag
news:bp53n1$4o7$1@news.island.liu.se…
Gavin Sinclair wrote:
Seriously, the ability to negative-index strings and arrays like that
is a breath of fresh air compared to traditional compiled languages.
I see no need to improve on it.
I do, but I have no good solution to the problem.
The problem being that positive indexes go from 0 to n-1, while negative
go from -1 to -n, which could introduce confusion.
But as I said, I know of no good solutions, since -0 is not a good way,
This reminds me of an old mathematician’s joke: there was this professor
for math that found an epsilon so small, that half of it was already
negative…
and making the first positive index a 1 is just as bad, for several
reasons.
Yup
robert
···
On Saturday, November 15, 2003, 2:48:53 AM, KONTRA wrote: