What's the simplest way to get only the last 10 bytes of a string?
buf[-10,10]
doesn't work if buf is smaller than 10 bytes, as it returns nil. Is there
anything simpler than
buf[-10,10] || buf
?
,From time to time I find myself trying to do something like
that. Maybe the best thing was if the String#slice method
were defined for using negative lengths but that's probably
too late.
For my personal use I wrote a litte extension doing exactly
that. You can have it if you want.
Inspite of its shortness it's surely buggy. Parameter
definition will ever be a matter of taste. Don't try to
convince the core developpers there's a neccessity to proffer
a solution for this. No chance.
Bertram
···
Am Freitag, 01. Jun 2007, 20:15:54 +0900 schrieb Brian Candler:
I think I'll stick with buf[-10,10] || buf. I was just wondering if I'd
missed something obvious like buf.last(10), after staring at 'ri String' too
long.
Finally I found a usage case where perl has the edge
ok, I have a question now. In the original post, he wanted to cover
errors caused by undersized strings. I have been trying to grok the
"zen of ruby" from posts in here and I thought that writing Ruby-ish
code involves two things: 1. making it so that it reads as plain
English and 2. Letting Ruby do the work for you.
I could do it easily enough in Pascal, for example:
s := @s[length(s) - 10];
but then we need to check for length errors as mentioned before.
Here comes the question, why was my approach wrong? I am assuming that
it was horrible because no one even bothered to tell me that I was being
a total goober by saying this:
I know that just having working code is not enough or we could use C#
and have done with it. How do we make it readable? How do we let Ruby
do the work?
It seems to me that approaches like str[/(.{1,10}\z)/] are neither plain
language readable nor letting Ruby do the work as it seems that the work
is in the coding.
Assuming that all the variations work equally well, how does one choose
which is the more "ruby like" in its approach? How can I tell which
approach is ugly and which is elegant?
I think I'll stick with buf[-10,10] || buf. I was just wondering if I'd
missed something obvious like buf.last(10), after staring at 'ri String' too
long.
That is a great answer too
I guess the OP didn't mention newlines, so \z works better.
···
On 6/2/07, Bertram Scharpf <lists@bertram-scharpf.de> wrote:
Hi,
Am Freitag, 01. Jun 2007, 22:10:51 +0900 schrieb Leonard Chin:
> On 6/1/07, Harry Kakueki <list.push@gmail.com> wrote:
> >str =~ /(.{1,10}$)/
>
> This works nicely if you do this:
> str[/(.{1,10}$)/]
module ActiveSupport #:nodoc:
module CoreExtensions #:nodoc:
module String #:nodoc:
# Makes it easier to access parts of a string, such as specific characters and substrings.
module Access #snip...
# Returns the last character of the string or the last +limit+ characters.
···
On Jun 2, 2007, at 10:07 AM, Lloyd Linklater wrote:
ok, I have a question now. In the original post, he wanted to cover
errors caused by undersized strings. I have been trying to grok the
"zen of ruby" from posts in here and I thought that writing Ruby-ish
code involves two things: 1. making it so that it reads as plain
English and 2. Letting Ruby do the work for you.
I could do it easily enough in Pascal, for example:
s := @s[length(s) - 10];
but then we need to check for length errors as mentioned before.
Here comes the question, why was my approach wrong? I am assuming that
it was horrible because no one even bothered to tell me that I was being
a total goober by saying this:
I know that just having working code is not enough or we could use C#
and have done with it. How do we make it readable? How do we let Ruby
do the work?
It seems to me that approaches like str[/(.{1,10}\z)/] are neither plain
language readable nor letting Ruby do the work as it seems that the work
is in the coding.
Assuming that all the variations work equally well, how does one choose
which is the more "ruby like" in its approach? How can I tell which
approach is ugly and which is elegant?
class String #:nodoc:
include ActiveSupport::CoreExtensions::String::Access
include ActiveSupport::CoreExtensions::String::Conversions
include ActiveSupport::CoreExtensions::String::Inflections
include ActiveSupport::CoreExtensions::String::StartsEndsWith
include ActiveSupport::CoreExtensions::String::Iterators
include ActiveSupport::CoreExtensions::String::Unicode
end
So I'd venture to say that the Ruby Way is to simply open up the String class and give it a new method. Since there is already an Array#first that gives [1,2,3,4].first(2) => [1,2], it just seems right to match that with .last and move the pair onto String treating the individual characters like the elements of the Array.
Although the longer-term would seem to be to reconcile the behavior with Ranges:
>> "abcde"[0...3]
=> "abc"
>> "abcde"[-3..-1]
=> "cde"
>> "abcdefghijklmno"[-10..-1]
=> "fghijklmno"
>> "abcdefghijklmno"[0...10]
=> "abcdefghij"
>> "abcd"[0...10]
=> "abcd"
It seems like a lot of Array methods would be better served as equivalents from Enumerable. #last is one of these.
(It won't nearly be as fast as a pure Array implementation, but it would be extremely useful.)
···
On 2007-06-04 10:40:32 -0700, Ryan Davis <ryand-ruby@zenspider.com> said:
On Jun 1, 2007, at 23:14 , Brian Candler wrote:
I think I'll stick with buf[-10,10] || buf. I was just wondering if I'd
missed something obvious like buf.last(10), after staring at 'ri String' too
long.
What would be the chances of getting something added to the core? It
seems to me that a new kind of slice would be in order. I know that
there are lTrim() and rTrim() (left and right) in other languages. What
about l_Slice and r_Slice? Then, it would be:
irb(main):001:0> str = "now is the time for all good men to come"
=> "now is the time for all good men to come"
irb(main):002:0> str[-10..-1]
=> "en to come"
irb(main):001:0> str = "now is the"
=> "now is the"
irb(main):002:0> str[-10..-1]
=> "now is the"
So I really see no need for a special method to obtain the last x
bytes of a string.
···
On Jun 12, 3:59 pm, Lloyd Linklater <l...@2live4.com> wrote:
> p 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.r_slice(0..9)
What about this:
def last_bytes(s, number_of_bytes)
if s.size > number_of_bytes then
return s.slice(s.size - number_of_bytes..s.size)
end
return s
end
p last_bytes('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
p last_bytes('WXYZ', 10)