2009/11/3 Just Another Victim of the Ambient Morality <
[email protected]=
m>:
message
What method doesn't work? If you mean the string[-index, index] method
it
does work fine for me. I am using 1.9.1 if that makes any difference.
The string[-index..-1] method does the same thing but I have used
Business
Basic for over 20 years so my method was easier for me.
In retrospect, I'm surprised I put it so harshly but it doesn't do the
same thing as the other method. Your solution requires that you know how
long a tail you need. If, instead, you know how much of the head you nee= d
to remove but don't know or care how long the tail is, your method is
insufficient...
The you can still do string[len..-1] or string.slice(len..-1) and do
not have to repeat the length. =A0I don't really understand what all the
fuzz is about. =A0You can get at the information you need and it's not
even difficult. =A0It's just not that there is an explicit method which
accepts a single parameter for the length which retrieves said portion
of the beginning or end.
The reason why there is probably not a #left or #right in String is
that often string manipulation is done via regular expressions anyway
instead of via indexes. =A0Personally I find solutions like s[/\w+\z/]
very elegant.
=A0 =A0What is up with my client that it randomly chooses to not quote?! = =A0I'm
sorry about this...
Maybe it's because you are using Microsoft Outlook Express...
=A0 =A0It's not a big fuss, it's just odd that a language that is filled = with
all sorts of other nicities doesn't have a simple solution to match a sim= ple
and common problem.
That's where I tried to argue that the problem might not be as common
as assumed. I have no overview about what all others do but - for
example - I cannot remember the last situation where I had to access
portions of a string based on indexes.
=A0 =A0To give another example, in some sense reversed, we all appreciate= how
Ruby has anonymous closures (blocks). =A0Python has closures too, they're= just
not anonymous and they are, thus, rather ugly to use. =A0Practically, thi= s is
meaningless since you can simply declare your closure (which is strangely
just a function) just in front of where you need it but I'm sure we all l= ook
at that and think the same thing: it would be nice if Python had anonymou= s
closures like Ruby!
=A0 =A0It's just a strange juxtaposition to see Ruby supply arrays with a= .last
method but not provide strings with a simple way of getting the tail,
especially since this is present in competing languages...
I see your point but I don't feel the same because I simply do not
need this feature plus I don't find the current ways to achieve it too
annoying to bother wondering why this isn't simpler.
=A0 =A0Finally, I could have sworn that you were the one advocating that = using
regular expressions for string manipulations that don't specifically requ= ire
their power to be unnecessarily dangerous! =A0Am I mistaken?
Um... I may have a blind spot there as I use regular expressions
regularly (you see, the name does make sense - at least for me
).
At the moment I don't really see the danger there. In fact, often I
find regular expressions less dangerous because if you specify a
pattern that you want to extract your code is more robust against
changing lengths:
irb(main):007:0> %w{foo bar much_too_long}.each do |s|
irb(main):008:1* printf "%-6s %s\n", s, Time.now
irb(main):009:1> end
foo 2009-11-03 09:49:31 +0100
bar 2009-11-03 09:49:31 +0100
much_too_long 2009-11-03 09:49:31 +0100
=3D> ["foo", "bar", "much_too_long"]
Now if you are going to extract the time based on the "fact" that it
must start at column 6 of the string you're in trouble. It does work
much better by doing str[/^\S+\s+(.*)$/, 1]. This is of course just a
silly example but I hope you get the idea.
Personally I dislike using indexes for such operations for another
reason: assume you have a program that writes out columns based on
defined width (say a log file). Assume further that you have a number
of tools that need to read those log files and extract data. If you
need to stuff more into a field of the log you can easily adjust the
writing application but you have to adjust all the readers as well!
If you have defined your format in terms of patters a simple extension
of a field's length does not require any change to reading programs.
Kind regards
robert
--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/