Click to See Complete Forum and Search --> : Ruby


Gnu/Vince
02-09-2001, 04:10 PM
Hey klamath, since you're the Ruby expert, could you show us how to do something simple? Like:

Ask for Name
Ask for Age
YearofBirth = CurrentYear - Age
Print "Your name is Name and you were bron in YearofBirth"

XxMaCaBrExX
02-09-2001, 04:19 PM
or how about:

Print "IMADUMBASS";

:P

osnap
02-09-2001, 05:15 PM
What I do know is a simple way of getting user input is something like:
def getName
print "What is your name? "
STDIN.gets.chomp!
end

then ask for it by putting getName somewhere and it will ask for your name.

To get the year you use something like this:

t = Time.now
t.year

and it will return the year it is.

I've been trying to get it to subtract the age from the year though and haven't figured it out yet.

osnap
02-09-2001, 05:33 PM
Here it is I figured it out, this is just a quick and dirty way i think and it's not very accurate because you need to ask for the month and day too.

#!/usr/bin/env ruby


def getName
print "What is your name? "
STDIN.gets.chomp!
end


def getAge
print "What is your age? "
STDIN.gets.chomp!
end


t = Time.now
name = getName
age = getAge
year = t.year


yearborn = year.to_i - age.to_i


print "Your name is #{name} and you were born in #{yearborn}\n"

[ 09 February 2001: Message edited by: osnap ]

jemfinch
02-09-2001, 06:18 PM
<the sound of me holding back>

:D

Jeremy

osnap
02-10-2001, 03:49 AM
Here's pretty much the same thing except I used a class.


#!/usr/bin/env ruby

class Getit
def getName
print "What is your name? "
STDIN.gets.chomp!
end

def getAge
print "What is your age? "
STDIN.gets.chomp!
end
end

it = Getit.new
name = it.getName
age = it.getAge
year = Time.now.year

yearborn = year.to_i - age.to_i

print "Your name is #{name} and you were born in #{yearborn}\n"