From: Robert Klemme Date: 2009-11-03T17:57:22+09:00 Subject: Re: How do you get the tail end of a string? 2009/11/3 Just Another Victim of the Ambient Morality : > > "Robert Klemme" wrote in message > news:2921fe3d-ba17-4cef-8e17-aed40ce9e253@m16g2000yqc.googlegroups.com... > On 2 Nov., 03:16, "Just Another Victim of the Ambient Morality" > wrote: >> "Michael W. Ryder" <_mwryde...@gmail.com> wrote in >> messagenews:d3oHm.2522$1R3.528@newsfe18.iad... >> >> >> >> > 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 need >> 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.  I don't really understand what all the > fuzz is about.  You can get at the information you need and it's not > even difficult.  It'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.  Personally I find solutions like s[/\w+\z/] > very elegant. >    What is up with my client that it randomly chooses to not quote?!  I'm > sorry about this... Maybe it's because you are using Microsoft Outlook Express... :-) >    It'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 simple > 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. >    To give another example, in some sense reversed, we all appreciate how > Ruby has anonymous closures (blocks).  Python has closures too, they're just > not anonymous and they are, thus, rather ugly to use.  Practically, this 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 look > at that and think the same thing: it would be nice if Python had anonymous > closures like Ruby! >    It'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. >    Finally, I could have sworn that you were the one advocating that using > regular expressions for string manipulations that don't specifically require > their power to be unnecessarily dangerous!  Am 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 => ["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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/