SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,658
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    method to find firsty letter of word?

    heres the method,

    def start_of_word(word,x)
    letter = word[0,x]
    letter
    end

    word[0,x]
    confused me but I think its creating an array, word and puts in it (starting at position/index 0) x letters, is this 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,999
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    I think its creating an array, word and puts in it (starting at position/index 0) x letters, is this right?
    Almost.

    Code Ruby:
    letter = word[0,x]

    Here the variable letter is a string, which is assigned characters 0 - x of the string "word".

    FWIW the value of the final expression evaluated in a method is the return value in ruby, so you could also write:

    Code Ruby:
    def start_of_word(word,x)
      word[0,x]
    end

  3. #3
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,658
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ok, thanks again
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •