From: Tom Sawyer Date: 2002-07-24T02:06:44+09:00 Subject: Re: Strings and member-operators On Tue, 2002-07-23 at 10:54, David Alan Black wrote: (namely, it would see that > [0] and [-1] added up to '""', which is a false positive if you're > looking for matching double quotes). true but it would still return the right result. (my fix at least) well, guess it goes to show. regexp are damn powerful, even if they can't solve every parsing problem, and are rather cryptic. yet this thread has made me desire a new String method. one that can pick out and join only particular characters: "thanks".pick(0, -1) # => "ts" ~transami > Hi -- > > On Wed, 24 Jul 2002, Tom Sawyer wrote: > > > let me get this straight? is the original question how to strip a string > > and remove quotes? > > > > /^("?)(.*)\1$/.match(self)[2].strip > > > > this only does double quotes and its striping after the quotes removal. > > is that what's wanted? well, i'll assume so. what about without regexp? > > > > return str[1..-1].strip if str[0..0] + str[-1..-1] == '""' > > > > hmmm... you'd think there'd be a real simple way to do this without > > regexp, can it get any simpler? > > Well, I find the regex version simpler :-) To take care of single > quotes, by the way, just change it to: > > /^(["']?)(.*)\1$/ > > But still, if you wanted to do it without using a regex, you could do > something like your code but you'd need an 'else' in there somewhere: > > class String > def stripQuotes > if self[0] == ?" && self[-1] == self[0] > self[1..-2].strip > else > strip > end > end > end > > > (I've formatted it to be runnable with Dossy's test suite.) > > My thinking in doing this: > > if self[0] == ?" && self[-1] == self[0] > > was to make modifying it easier: > > if %q{' "}.include?(self[0]) ... > > (which is probably stupid, since it's not like I'm really > parameterizing it), and also to avoid the '+' operation because that > runs into problems with an empty string. In fact I think it would > also give weird results with the string '"' (namely, it would see that > [0] and [-1] added up to '""', which is a false positive if you're > looking for matching double quotes). > > > David > > -- > David Alan Black > home: dblack@candle.superlink.net > work: blackdav@shu.edu > Web: http://pirate.shu.edu/~blackdav > > -- ~transami