What does this do?

A

Abder-Rahman Ali

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

James Harrison

I have this portion of code from "Why's poignant guide to Ruby" book.
=20
str =3D "A string is a long shelf of letters and spaces" # Nice = definition
:)
puts str[0..-1]
puts str[1..-2]
=20
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.
=20


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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman Ali <
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
:)
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.
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.html>If 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*.
 
A

Abder-Rahman Ali

Josh said:
On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman Ali <
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.html>If 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.
 
H

Hassan Schroeder

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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman Ali <
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.html>If 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.
[/QUOTE]

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
Fixnum<http://ruby-doc.org/core/classes/Fixnum.html>objects, 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.
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Afternoon,

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.

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
 
A

Abder-Rahman Ali

Robert said:
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.
 

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
474,001
Messages
2,570,249
Members
46,847
Latest member
FlorrieSha

Latest Threads

Top