Forum: Ruby Calling functions

0dd83cf06f6fa4ef9d19e2d4de67082e?d=identicon&s=25 Richard Curteis (3therk1ll)
on 2013-11-04 17:23
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###
48aef6c74ff21bfe894b201c7936fcdd?d=identicon&s=25 Nation, Carey (Guest)
on 2013-11-04 17:43
(Received via mailing list)
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.
0dd83cf06f6fa4ef9d19e2d4de67082e?d=identicon&s=25 Richard Curteis (3therk1ll)
on 2013-11-04 17:51
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 method
`age' for main:Object (NameError)
  from ./revtest.rb:14:in `output'
  from ./revtest.rb:57:in `<main>'

Is this to do with with the variable 'age', not being exported from the
output function?
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-11-04 17:57
(Received via mailing list)
On Mon, Nov 4, 2013 at 5:51 PM, Richard Curteis <lists@ruby-forum.com>
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.
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2013-11-04 17:59
(Received via mailing list)
Subject: Re: Calling functions
  Date: Mon 04 Nov 13 05:51:12PM +0100
Sorry for the delay!

Quoting Richard Curteis (lists@ruby-forum.com):

> 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
0dd83cf06f6fa4ef9d19e2d4de67082e?d=identicon&s=25 Richard Curteis (3therk1ll)
on 2013-11-04 18:02
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 "Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> (Guest)
on 2013-11-04 18:19
(Received via mailing list)
On Mon, Nov 4, 2013 at 6:02 PM, Richard Curteis <lists@ruby-forum.com>
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.
0dd83cf06f6fa4ef9d19e2d4de67082e?d=identicon&s=25 Richard Curteis (3therk1ll)
on 2013-11-04 18:22
Ok, I'll try that out, thanks for the help!
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.