Hello.
I noticed today that it is very easy to get the first few characters of
a String:
string[0…whatever]
however, it is harder to get the last few characters of a String
string[-8…-1] || string
Feature request/proposal:
String#last(n)
like
string.last(8)
Feedback welcome.
-roger-
On Fri, Jan 20, 2012 at 11:47, Roger P. [email protected]
wrote:
Feature request/proposal:
String#last(n)
If you think you really need it, you can just monkeypatch it in. Then
if you are finding it very useful, you can request it be added to the
class as distributed.
-Dave
On Fri, Jan 20, 2012 at 8:47 AM, Roger P. [email protected]
wrote:
Feature request/proposal:
There’s always the two argument form of slice:
“my string”[-3, 3]
=> “ing”
Not quite as nice as yours, however.
pete
On 01/20/2012 02:52 PM, Pete H. wrote:
On Fri, Jan 20, 2012 at 8:47 AM, Roger P.[email protected] wrote:
string[-8…-1] || string
…
There’s always the two argument form of slice:
“my string”[-3, 3]
=> “ing”
And it still has this “feature”:
“123”[-5,5]
=> nil
You should add this request to the bug tracker to ruby.
I don’t think matz and many others regularly read every mail here. 
I do agree with you - I think .last(5) would be nice and more readable
than “foobar”[-5,5] # => “oobar”
“foobar”[-5,5] # => “oobar”
“foobar”.last 5 # => “oobar”
But I also must say, I am now so used to use [] that I don’t use
anything else. For the record, I also don’t use .slice
I just love [] too much.
however, it is harder to get the last few characters of a String
string[-8…-1] || string
You could try something like this.
str = “123456789”
p str[/.{1,8}\z/]
p str[/\A.{1,8}/]
Harry
Hello.
I noticed that it is very easy to get the first few characters of
a String:
string[0…whatever]
however, it is “harder” to get the last few characters of a String
ex: to get the last 8 you have to do something like:
(string[-8…-1] || string)
Feature request/proposal:
String#last(n)
like
string.last(8)
and related
String#first(n)
Feedback welcome. If none I’ll file it on redmine with a patch.
-roger-
Em 27-01-2012 14:00, Roger P. escreveu:
(string[-8…-1] || string)
Feedback welcome. If none I’ll file it on redmine with a patch.
-roger-
I like the idea. I’m just not sure if ‘last’ would be clear enough…
Don’t have another suggestion for now, sorry…
+1
I think these are good methods too. They are defined in Ruby F.s, but
in
a bit different way then one would expect.
http://rubyworks.github.com/rubyfaux/?doc=http://rubyworks.github.com/facets/docs/facets-2.9.3/core.json#api-module-Indexable
I’d like to go whole hog here and propose Ruby adopt the Indexable
module.
and related
String#first(n)
Feedback welcome. If none I’ll file it on redmine with a patch.
-roger-
I like the idea. I’m just not sure if ‘last’ would be clear enough…
Don’t have another suggestion for now, sorry…
Hmm. I was thinking “first” and “last” would be good since they would
match Array#last and Array#first but maybe something like first_chars
or something?
On Mon, Jan 30, 2012 at 16:27, Roger P. [email protected] wrote:
Hmm. I was thinking “first” and “last” would be good since they would
match Array#last and Array#first but maybe something like first_chars
or something?
For #first, since String#chars returns an enumerator, you can currently
get
away with str.chars.first(10). But since Enumerator#last doesn’t exist,
perhaps that’s argument enough for having #{first,last}_chars.
Em 30-01-2012 14:27, Roger P. escreveu:
and related
String#first(n)
Feedback welcome. If none I’ll file it on redmine with a patch.
-roger-
I like the idea. I’m just not sure if ‘last’ would be clear enough…
Don’t have another suggestion for now, sorry…
Hmm. I was thinking “first” and “last” would be good since they would
match Array#last and Array#first but maybe something like first_chars
or something?
I find first_chars and last_chars much more readable. But that is my
personal opinion, feel free to check with others too.
I’d like throw my hat in for this too, but in a more general way. I may
have mentioned this once before, but it is possible to generalize a
number
of methods like #first and #last in much the same way that Enumerable
and
Comparable does for other methods. I’d rather see this general purpose
Indexable
module which includes #last and #first and related methods.
See http://rubydoc.info/github/rubyworks/facets/master/Indexable