What does this do?

I have this portion of code from “Why’s poignant guide to Ruby” book.

str = “A string is a long shelf of letters and spaces” # Nice definition
:slight_smile:
puts str[0…-1]
puts str[1…-2]

I just want to ask. Does the preceding code REMOVE what is BETWEEN the
range? I just concluded that from the result I get and want to make
sure.

Thanks.

On Jul 13, 2010, at 3:26 PM 7/13/10, Abder-Rahman A. wrote:

I have this portion of code from “Why’s poignant guide to Ruby” book.

str = “A string is a long shelf of letters and spaces” # Nice definition
:slight_smile:
puts str[0…-1]
puts str[1…-2]

I just want to ask. Does the preceding code REMOVE what is BETWEEN the
range?

no

I just concluded that from the result I get and want to make
sure.

I’ve seen you a couple of times today. Do you know what irb is? I don’t
think you do, but I want to make sure. It’ll help reading why’s guide
(which is where I learned ruby too…)

On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman A. <
[email protected]> wrote:

Thanks.

Posted via http://www.ruby-forum.com/.

I’m not really sure what you’re asking, but I think that it is probably
answered by the docs:

http://ruby-doc.org/core/classes/String.html#M000771

str[range] => new str or nil

http://ruby-doc.org/core/classes/String.src/M000771.htmlIf given a
range,
a substring containing characters at offsets given by the range is
returned.
In all three cases, if an offset is negative, it is counted from the end
of
str.

Josh C. wrote:

On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman A. <
[email protected]> wrote:

Thanks.

Posted via http://www.ruby-forum.com/.

I’m not really sure what you’re asking, but I think that it is probably
answered by the docs:

http://ruby-doc.org/core/classes/String.html#M000771

str[range] => new str or nil

http://ruby-doc.org/core/classes/String.src/M000771.htmlIf given a
range,
a substring containing characters at offsets given by the range is
returned.
In all three cases, if an offset is negative, it is counted from the end
of
str.

Thanks Josh.

For example, this from the documentation:
a = “hello there”
a[-3,2] #=> “er”

-3 —> Start counting from 0 to 3 from the end (Right -> Left)
In this case I reached the letter “h” of there

2 —> What should I do here?

Thanks.

Hi James. Of course I know irb (Interactive Ruby). I can use it from the
terminal on MAC OS X, and even online here: http://tryruby.org/

On Tue, Jul 13, 2010 at 3:09 PM, Abder-Rahman A.
[email protected] wrote:

For example, this from the documentation:
a = “hello there”
a[-3,2] #=> “er”

-3 —> Start counting from 0 to 3 from the end (Right -> Left)
In this case I reached the letter “h” of there

2 —> What should I do here?

Open up irb and experiment until it’s clear :slight_smile:

On Tue, Jul 13, 2010 at 5:09 PM, Abder-Rahman A. <
[email protected]> wrote:

I’m not really sure what you’re asking, but I think that it is probably
a substring containing characters at offsets given by the range is

Posted via http://www.ruby-forum.com/.

The first character is zero, the last character is negative one (there
is no
negative zero), when you count backwards from the end:
-1: e
-2: r
-3: e
-4: h
-5: t

So, as the docs say “If passed two
Fixnumhttp://ruby-doc.org/core/classes/Fixnum.htmlobjects, returns a
substring starting at the offset given by the first, and
a length http://ruby-doc.org/core/classes/String.html#M000774 given by
the
second.”
-3,2 puts you at the e in -3, and returns two characters forward from
that
spot. If you are having a difficult time seeing what you could expect,
try
giving different numbers in irb, as James suggested.

Open IRB, say “if this thing the docs are telling me is correct, and I
correctly understand it, then I expect xxx to happen when I yyy” Then
yyy,
and see if you get xxx. If you do, that means you probably understand it
right. If not, you probably need to experiment more to get a better idea
of
what is happening. You can try following patterns to see how the results
change with the pattern:
a[-4,2]
a[-5,2]
a[-6,2]
a[-6,3]
a[-6,4]

Find the patterns in the output, make a hypothesis, see if it gives you
what
you expected.

Afternoon,

At some point you have to at least make some attempt to understand
something here. IRB is again your friend here. While I get that you are
learning this - you cannot just sit there like a lump on a log and
expect
things to be given to you over and over again.

Would you please at least try some variants of things to see if you are
correct. For example,

if a[-3,2] returns a string of 2 characters what maybe does the 2 mean?
How
about trying [-3,3] and seeing how many characters you get back. That
might
be a great way to validate the obvious answer as to what the 2 means.

Now as to the -3 maybe you aren’t counting correctly - so how about
trying
this

a = “987654321”

a[-1, 1]
a[-2, 2]
a[ -3, 3]

And see what you get. The answers are very self obvious - but you really
have to put in at least some effort here… Reading a book and never
even attempting any concepts just won’t work!

John

Thanks a lot Josh for your thorough clarification. This really helps.

Thanks everyone for your replies. Yes, things need experimenting with.

Robert K. wrote:

On 13.07.2010 23:57, Abder-Rahman A. wrote:

Hi James. Of course I know irb (Interactive Ruby). I can use it from the
terminal on MAC OS X, and even online here: http://tryruby.org/

Great! Then you can try these things out. In my experience knowledge
gained through one’s own experimentation is more solid than secondhand
knowledge anyway.

Kind regards

robert

That’s right Robert. Thanks.

On 13.07.2010 23:57, Abder-Rahman A. wrote:

Hi James. Of course I know irb (Interactive Ruby). I can use it from the
terminal on MAC OS X, and even online here: http://tryruby.org/

Great! Then you can try these things out. In my experience knowledge
gained through one’s own experimentation is more solid than secondhand
knowledge anyway.

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs