Calling functions

Hey guys, totally new to Ruby so bear with me please.

I can’t get the functions to call is a little script I’m trying to get
to run, just some basic input output to learn my way around Ruby.

###CODE START###

#!/usr/bin/ruby

def output()
age=27
name=“Max”
weight=96

print “Age: #{age}\n”
print “\n”
print “Name: #{name}\n”
print “\n”
print “Weight: #{weight}kg\n”

end

def ageChecker()
print “\n”
print "Enter age: "
ageInput=Integer(gets.chomp)
print “You entered your age as: #{ageInput}\n”

if ageInput == age
print “Correct age selected.\n”
nameChecker

elsif ageInput > age
print “You are younger than that, you are #{age}\n”
ageChecker

else ageInput < age
print “You are older than that, you are #{age}\n”
ageChecker
end

end

def nameChecker()
print “\n”
print "Enter name: "
nameInput=String(gets.chomp)
nameInput.capitalize!
print
print “You set your name as: #{nameInput}\n”
print “\n”

if nameInput == name
print “Correct\n”

else
print “Wrong name\n”
nameChecker

end
end

output

###CODE END###

You only call the output function at the bottom of your script. It does
what it does and then you exit. If you want the other stuff to happen,
you have to call it too.

Don’t know how I missed that, I have ageChecker in the version I’m using
here but it still returns the following error after returning what the
user enters as age.

./revtest.rb:23:in ageChecker': undefined local variable or methodage’ for main:Object (NameError)
from ./revtest.rb:14:in output' from ./revtest.rb:57:in

Is this to do with with the variable ‘age’, not being exported from the
output function?

Subject: Re: Calling functions
Date: Mon 04 Nov 13 05:51:12PM +0100
Sorry for the delay!

Quoting Richard C. ([email protected]):

output function?
You should carefully study the part of whatever tutorial or book you
are using, where it describes the scope of variables. The variables
you define in ‘output’ can only be seen from within that method. To
have variables with external visibility you should either use global
variables (prepend a ‘$’ to the variable name), or, better,
encapsulate your example in a class and use class instance variables
(prepended with a ‘@’).

Carlo

Ok, thanks. But when I then move on to nameChecker from ageChecker, the
variable name won’t have been passed, do I have to pass it first to
ageChecker and then onwards or can I simply set them as global variables
to save time?

Edit: Hadn’t seen that last post. Thanks.

On Mon, Nov 4, 2013 at 5:51 PM, Richard C. [email protected]
wrote:

Is this to do with with the variable ‘age’, not being exported from the
output function?

Yes, variables are local to their scope. In your case, age is local to
the output method, once the method finishes, it ceases to exist.
Maybe you can have the variable age outside of the methods and pass it
as a parameter to them?

age = 27
ageChecker(age)

Jesus.

On Mon, Nov 4, 2013 at 6:02 PM, Richard C. [email protected]
wrote:

Ok, thanks. But when I then move on to nameChecker from ageChecker, the
variable name won’t have been passed, do I have to pass it first to
ageChecker and then onwards or can I simply set them as global variables
to save time?

I think it’s cleaner that each method does only one thing, and have
the code at the upper level call one, then the other, etc.
Check this for example:

def output(age, name)
weight=96

print “Age: #{age}\n”
print “\n”
print “Name: #{name}\n”
print “\n”
print “Weight: #{weight}kg\n”

end

def prompt(message)
print message
return gets.chomp
end

def ageChecker(ageInput, age)
if ageInput == age
print “Correct age selected.\n”
return true
elsif ageInput > age
print “You are younger than that, you are #{age}\n”
return false
else ageInput < age
print “You are older than that, you are #{age}\n”
return false
end

end

def nameChecker(nameInput,name)
if nameInput == name
print “Correct\n”
return true
else
print “Wrong name\n”
return false
end
end

age = 27
name = “Max”
output(age, name)
begin
ageInput = Integer(prompt "Enter your age: ")
end while (!ageChecker(ageInput, age))

begin
nameInput = prompt "Enter your name: "
nameInput.capitalize!
end while (!nameChecker(nameInput, name))

There are other ways of organizing the code to be cleaner. For
example, have the age and name checkers only do their logic, without
the prints, and have all the output in the top level.

Jesus.

Ok, I’ll try that out, thanks for the help!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs