Gwater17
Gwater17

Reputation: 2314

Calling methods within methods ruby

I'm trying to figure out how to call methods inside methods in ruby.

Here's my code:

def testNegative number 
  if number < 0  # No negative numbers.
    return 'Please enter a number that isn\'t negative.'
  end
end

def testIfZero number
  if number == 0
    return 'zero'
  end
end

def englishNumber number
  testNegative(number) 
  testIfZero(number) 
end

puts englishNumber -1
puts englishNumber 0

Currently, the output I'm getting is an empty line and then "zero". I was wondering why I don't see "Please enter a number that isn't negative" as the output of put englishNumber -1. How can I fix things so that "Please enter a number that isn't negative" is returned and the programs ends?

Upvotes: 0

Views: 89

Answers (2)

David Grayson
David Grayson

Reputation: 87406

I'll use underscores instead of camel case in my answer, because that is what people usually do for Ruby methods.

Your code doesn't work right now because test_negative is returning a value but english_number is not doing anything with the value. You could change english_number to:

def english_number(number)
  test_negative(number) or test_if_zero(number)
end

That way if test_negative returns a non-nil and non-false value, then english_number will use that as its return value and not bother running test_if_zero.

Later, if you need to add more lines of code to english_number, you can do it like this:

def english_number(number)
  error_message = test_negative(number) || test_if_zero(number)
  return error_message if error_message

  # More lines of code here
end

By the way, it sounds like you might actually want to use exceptions. When you raise an exception, the program will end and the exception message will be printed to the user unless you do something to catch and handle the exception. I would change test_negative to:

def test_negative(number)
  if number < 0  # No negative numbers.
    raise 'Please enter a number that isn\'t negative.'
  end
end

Upvotes: 1

knut
knut

Reputation: 27855

Your code

def englishNumber number
  testNegative(number) 
  testIfZero(number) 
end

calls first testNegative(number) and ignores the result.

Then it calls testIfZero(number) and returns the result to the caller.

For englishNumber -1 the result of testIfZero(number)is nil and putswrites an empty line.

For englishNumber 0 you get the expected string 'zero'.


If you need a the results as alternative you must return the results with an or condition (or ||).

A complete example:

def testNegative number 
  if number < 0  # No negative numbers.
    return 'Please enter a number that isn\'t negative.'
  end
end

def testIfZero number
  if number == 0
    return 'zero'
  end
end

def englishNumber number
  testNegative(number) or testIfZero(number) 
end

puts englishNumber -1
puts englishNumber 0

or with an alternate syntax:

def testNegative number 
    return 'Please enter a number that isn\'t negative.' if number < 0  # No negative numbers.
end

def testIfZero number
  return 'zero' if number == 0
end

def englishNumber number
  testNegative(number) ||  testIfZero(number) 
end

puts englishNumber -1
puts englishNumber 0

I'm not sure if I understand your comments correct, but maybe you are looking for a solution like this:

def testNegative number 
    puts 'Please enter a number that isn\'t negative.' if number < 0  # No negative numbers.
end

def testIfZero number
  puts 'zero' if number == 0
end

def englishNumber number
  testNegative(number) 
  testIfZero(number) 
end

englishNumber -1
englishNumber 0

The puts command can also be used inside the methods, you don't need to return a result.


Remark outside the scope of the question: The usage of a method call inside a method is not the simplest solution (but I think you want to check the usage of method calls).

I would use:

def english_number number

  if number < 0  # No negative numbers.
    return 'Please enter a number that isn\'t negative.'
  elsif number == 0
    return 'zero'
  end
end

Upvotes: 0

Related Questions