How does the slice method work? If I want to write:
x.slice(parameter 1, parameter 2)
what do the parameters specify in terms of what part of x (say it’s a
sentence) the program slices?
Thanks!
How does the slice method work? If I want to write:
x.slice(parameter 1, parameter 2)
what do the parameters specify in terms of what part of x (say it’s a
sentence) the program slices?
Thanks!
On Tue, Feb 15, 2011 at 5:46 PM, Gaba L.
[email protected] wrote:
How does the slice method work? If I want to write:
x.slice(parameter 1, parameter 2)
what do the parameters specify in terms of what part of x (say it’s a
sentence) the program slices?
ri String#slice Array#slice
robert
Thanks - I’m still a little bit confused. Where does the slice
begin/stop slicing?
It selects the number of elements given by the second parameter,
starting at the index given by the first parameter.
On Tue, Feb 15, 2011 at 11:15 AM, Gaba L.
Thanks - very clear!
You pick a starting point to slice and then a number of items to
slice. For example, if you write:
“0123456789”.slice(i, size)
then you will slice starting after the i
-th character, and for
size
more characters, or until the end of the sequence, whichever
comes first. For example,
“0123456789”.slice(5, 3)
will yield “567”. We start at “5” because the i = 5th character is
“4”, and “5” is right after that. Then we take size = 3 characters in
all, yielding “567”.
By contrast, if you wrote,
“abcdefgh”.slice(5, 10)
you will get “fgh”, because we can’t take any more characters after
“h” from the sequence.
John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs