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?
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 Jul 13, 2010, at 3:26 PM 7/13/10, Abder-Rahman Ali wrote:
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 <class String - RDoc Documentation; 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.
ยทยทยท
On Tue, Jul 13, 2010 at 5:09 PM, Abder-Rahman Ali < abder.rahman.ali@gmail.com> wrote:
Josh Cheek wrote:
> On Tue, Jul 13, 2010 at 4:26 PM, Abder-Rahman Ali < > > abder.rahman.ali@gmail.com> 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:
>
>
>
> class String - RDoc Documentation
>
> 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
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!
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
ยทยทยท
On 13.07.2010 23:57, Abder-Rahman Ali 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/
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.