SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: im confused...

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    im confused...

    Im kind of confused on a few things in ruby...

    "Hello".length(4)
    returns l
    because H is 1, e is 2, l is 3, and another l is 4
    but
    my_Array = [1, 2, 3, 4, 5]
    my_Array[2]
    returns a 3 cause arrays are 0 indexed.
    my_Array.count
    returns 5

    when I use String.length, why does the index not start at 0, but it does with arrays?
    Is that even right?
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    "Hello".length(4)
    returns l
    Does it?

    AFAIK, you cannot pass an argument to length()
    http://ruby-doc.org/core-2.0/String....ethod-i-length

    Using Ruby 1.9.3

    Code:
    p "Hello".length(4)
    hello.rb:1:in 'length': wrong number of arguments(1 for 0) (ArgumentError)
    however:

    Code:
    p "Hello".length
    => 5
    Quote Originally Posted by lukeurtnowski View Post
    when I use String.length, why does the index not start at 0, but it does with arrays?
    Both strings and arrays are zero indexed.

    Code Ruby:
    hello = "Hello"
    p hello.length
    => 5
     
    array = [1, 2, 3, 4, 5]
    p array.length
    => 5
     
    p hello[0]
    => "H"
     
    p array[0]
    => 1
     
    p hello[4]
    => "o"
     
    p array[4]
    => "4"

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,582
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Its confusing cause hello.length = 5
    but hello[4] = o
    I guess its best to memorize whenever I use arrays (or array notation), everything starts at 0
    whereas the .length things starts counting at 1
    "Oh, and Jenkins--apparently your mother died this morning."

  4. #4
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,285
    Mentioned
    181 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    Its confusing cause hello.length = 5
    but hello[4] = o
    I guess its best to memorize whenever I use arrays (or array notation), everything starts at 0
    whereas the .length things starts counting at 1
    You actually just answered your question. length is returning the length of the object.
    If it is a string with 5 letters it will return 5. If it is an array with 9 items it will return 9.
    Whenever you call on the index of a string (or array) you are calling each element based on its index; which starts with zero.

    That means that a string with length = 5 will have indices of 0, 1, 2, 3, 4 (a total of 5)
    And a loop would be zero to length minus 1 (a very common construct in most programming languages.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •