Forum: Ruby get remaing characters in a string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B8d11b4c89cfc6e34d6c9fb121c9acac?d=identicon&s=25 Claus Guttesen (Guest)
on 2007-01-07 01:22
(Received via mailing list)
Hi.

I need to get the last three characters in a string. I couldn't find a
function in the String class so I came up with this:

class Remainder

 attr_writer :n

 def initialize(t = "", n = 0)
   @t = t.to_s
   @n = n.to_i
 end

 def get
   get_remaining_letters
 end

 private
 def get_remaining_letters
   s = @t.size
   if s < @n
     r = 0
   else
     r = s - @n
   end

   ft = @t.slice(r, @n)
 end

end


t = Remainder.new("Guttesen", 5)
puts t.get
tesen
t.n = 4
puts t.get
esen

Did I reinvent the wheel or does it make sense to add it to String?

regards
Claus
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian Gross (Guest)
on 2007-01-07 01:26
(Received via mailing list)
On 2007-01-07 01:21:54 +0100, "Claus Guttesen" <kometen@gmail.com> said:

> Hi.
>
> I need to get the last three characters in a string. I couldn't find a
> function in the String class so I came up with this:
>
> Did I reinvent the wheel or does it make sense to add it to String?

I'd just do str.slice!(-n .. -1) instead:

str = "Hello World"
str.slice!(-3 .. -1) # => "rld"
str # => "Hello Wo"
str.slice!(-1 .. -1) # => "o"
str # => "Hello W"
Cd3312ac93f768b1b449af457c0fca06?d=identicon&s=25 Daniel Finnie (Guest)
on 2007-01-07 01:27
(Received via mailing list)
You can use String#[] with a Range instead:
 >> "asdfhsdfabc"[-3..-1]
=> "abc"

or you can specify the length:
 >> "asdfhsdfabc"[-3, 3]
=> "abc"

Negative numbers count from the end of the string.  So basically the
strings length plus your negative number is the position from the start
of the string.

Dan
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-01-07 01:29
(Received via mailing list)
On Sun, 7 Jan 2007, Claus Guttesen wrote:

>  @t = t.to_s
>  if s < @n
>
> t = Remainder.new("Guttesen", 5)
> puts t.get
> tesen
> t.n = 4
> puts t.get
> esen
>
> Did I reinvent the wheel or does it make sense to add it to String?

You can index strings from the right with negative numbers:

   "abcdef"[-3..-1]   => "def"

I think that might be all you need.


David
B8d11b4c89cfc6e34d6c9fb121c9acac?d=identicon&s=25 Claus Guttesen (Guest)
on 2007-01-07 01:37
(Received via mailing list)
> > I need to get the last three characters in a string. I couldn't find a
> > function in the String class so I came up with this:
> >
> You can index strings from the right with negative numbers:
>
>    "abcdef"[-3..-1]   => "def"
>
> I think that might be all you need.

I see. Thank you very much for all the replies.

regards
Claus
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2007-01-07 09:21
(Received via mailing list)
Claus Guttesen wrote:
>    @t = t.to_s
>    if s < @n
>
> Claus
If the length of the string is less than 3,
the entire string will be returned.

s = "ab"
s[ /.{0,3}$/ ]
s[-3,3] || s
This topic is locked and can not be replied to.