Thread: New to Ruby

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    New to Ruby


    I am hoping for some help, I am trying to self teach Ruby, I have previously taken many Java Classes but that does not seem to be helping me Here is what I need to do, it is not for a regular class so I am not trying to get others to do my homework.

    1. Implement the following as a command line program in ruby.
    A. The program must accept two inputs, the first being a name, and the second being the year of birth
    B. The program will the output the sentence "Hello PROVIDED_NAME, you are YEARS_OLD."
    C. The PROVIDED_NAME will be the name entered into the program and YEARS_OLD will be the result of the subtraction of the current year from the year of birth.
    D. The program must be able to run in any year and give the correct value (you cannot hard code 2010 as the current year).


    I have the hello name part, I cannot get it to do the calculations for me. I have tried looking up samples to get an idea but am stuck. I appreciate any help!!!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Show us what you have so far and we will comment.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0
    Originally Posted by L7Sqr
    Show us what you have so far and we will comment.
    I'm pretty sure that I am so far off you guys are going to laugh but here is what I have.

    def age(dob)
    now = Time.2010
    # how many years? 30
    # subtract 1 if so, 0 if not
    age = now.2010 - dob.1979 - (dob.to_time.change(:year => now.year) >
    now ? 1 : 0)
    end


    I am needing it to subtract 2010 from 1979
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0
    Here is the rest I have to do, to show I am trying to do it myself. I have to show hamming distance in a language of my choice, which I chose Java.

    def hamming(s1 ,s2)
    dist = 0
    Range.new(0,s1.length-1).each {|i| dist += 1 unless s1[i] == s2[i] }
    dist
    end


    I am now trying to write the same in Ruby, don't need help just showing that I am doing work on my own
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0
    If you're looking to grab someones age from a year of birth all you need to do is subtract the year of birth from the current year. If you look at the Time class[1] you'll see it contains the instance method #year

    Code:
    >> def age(dob) 
    >>   Time.new.year - dob 
    >> end 
    => nil 
    >> age 1981 
    => 29 
    >> age 1963
    => 47
    For gathering input check out IO#gets[2] and String#chomp[3]

    1. ruby-doc.org/core/classes/Time.html
    2. ruby-doc.org/core/classes/IO.html#M002272
    3. ruby-doc.org/core/classes/String.html#M000819
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0
    Thank You, I bookmarked those pages for future reference. I got all of the rest of it done, this was the only one that stumped me. I appreciate the help.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Association between classes


    Hey
    Im new in RoR,
    im doing relation between two classes, and it work prefectly.
    So the classe are Task and Story, and the relatin / association are, a Story ha-many tasks and task belongs_to stories
    what i wont is when i delete one story all tasks related with that story alsso must be deleted, also be able to see stories without tasks.
    Please can someone help me?
    please am realy stack
    Durival
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Smile dropdown menu


    HI How to do drop down menu using RoR
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0
    hey i also want to know about ror

IMN logo majestic logo threadwatch logo seochat tools logo