Getting the last N bytes of a string:

B

Brian Candler

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

?

Thanks,

Brian.

P.S. To get the *first* 10 bytes of a string is easy: buf[0,10] always
works, whether or not buf is smaller than 10 bytes)
 
H

Harry Kakueki

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

?

Thanks,

Brian.

P.S. To get the *first* 10 bytes of a string is easy: buf[0,10] always
works, whether or not buf is smaller than 10 bytes)

I don't know if this is simple but you can try something like this.

str =~ /(.{1,10}$)/

Harry
 
D

David Mullet

Brian said:
What's the simplest way to get only the last 10 bytes of a string?

There are several ways. One option is to pad the string with spaces,
extract your slice, then strip the leading and trailing whitespace:

buf = "abcdef"

buf.rjust(10, ' ')[-10, 10].strip

=> "abcdef"


David

http://rubyonwindows.blogspot.com
 
L

Lloyd Linklater

Brian said:
What's the simplest way to get only the last 10 bytes of a string?

Ok. Here is my first attempt at answering a coding question. /deep
breath

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.reverse.slice(0..9).reverse
 
S

Stefan Mahlitz

Brian said:
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

I'd use:

buf[-10..-1] || buf

but this is only a variation of what you have.

There is buf.reverse[0..9].reverse as well, but this looks even uglier
to me than the first.

Stefan
 
H

Harry Kakueki

str =~ /(.{1,10}$)/

This works nicely if you do this:
str[/(.{1,10}$)/]

Though I guess it depends on what you consider "simple".

Well, I guess the parentheses are not necessary.
This looks a little less cluttered.

str[/.{1,10}$/]

Harry
 
B

Bertram Scharpf

Hi,

Am Freitag, 01. Jun 2007, 20:15:54 +0900 schrieb Brian Candler:
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.

http://www.bertram-scharpf.de/tmp/bs-ruby.tar.gz

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
 
B

Brian Candler

Thanks everyone for your answers.

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 :)

$ perl -e '$a="abc";print substr($a,-10),"\n"'
abc
$ perl -e '$a="abcdefghijklmnop";print substr($a,-10),"\n"'
ghijklmnop

Regards,

Brian.
 
L

Lloyd Linklater

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:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.reverse.slice(0..9).reverse

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?
 
R

Rob Biedenharn

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:

'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.reverse.slice(0..9).reverse

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?

Well, this is how Rails solves this in ActiveSupport

vendor/rails/activesupport/lib/active_support/core_ext/string/access.rb

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.
#
# Examples:
# "hello".last # => "o"
# "hello".last(2) # => "lo"
# "hello".last(10) # => "hello"
def last(limit = 1)
(chars[(-limit)..-1] || self).to_s
end
end
end
end
end

Which is mixed into String by:
vendor/rails/activesupport/lib/active_support/core_ext/string.rb

require File.dirname(__FILE__) + '/string/inflections'
require File.dirname(__FILE__) + '/string/conversions'
require File.dirname(__FILE__) + '/string/access'
require File.dirname(__FILE__) + '/string/starts_ends_with'
require File.dirname(__FILE__) + '/string/iterators'
require File.dirname(__FILE__) + '/string/unicode'

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"
=> nil

If only that were also "abcd".

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Ryan Davis

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.

you could do that too:
=> "blah"

or do what you were looking for:
class String; def last(n=1); self[-n, n] || self; end; end => nil
"blah".last(10) => "blah"
"blah blah".last(4)
=> "blah"
 
E

Erik Hollensbe

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.

you could do that too:
=> "blah"

or do what you were looking for:
class String; def last(n=1); self[-n, n] || self; end; end => nil
"blah".last(10) => "blah"
"blah blah".last(4)
=> "blah"

"blah blah blah".to_a.last(4).join as well.

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.)
 
L

Lloyd Linklater

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:

p 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.r_slice(0..9)
 
B

Bertram Scharpf

Hi,

Am Dienstag, 12. Jun 2007, 23:50:36 +0900 schrieb Lloyd Linklater:
What would be the chances of getting something added to the core?

This is what I would have liked to propose.

If anyone understood the `notempty?' proposal.

Bertram
 
L

Lloyd Linklater

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)

this is the result:

"QRSTUVWXYZ"
"WXYZ"
 
B

bbiker

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)

this is the result:

"QRSTUVWXYZ"
"WXYZ"

Sorry to come so late in the thread.

This seems to work fine for me.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top