DEV Community 👩‍💻👨‍💻

changj35513
changj35513

Posted on

Methods in Methods and Parameters

If you need to use a method inside of another method, you need to make sure the right number of arguments are available.

In this case, there is a key name (that will be provided by the assignment checker), but you need it to replace the parameter (layers) for the method you called on (preparation_time_in_minutes) because where else are you going to get the value from?

Copied from Exercism:
"Units of functionality are encapsulated in methods - similar to functions in other languages.

A method can be defined with positional arguments, keyword arguments (which are defined and called using the : syntax) or have no arguments at all.

Methods either implicitly return the result of the last evaluated statement, or can explicitly return an object via the return keyword."

class Lasagna

  EXPECTED_MINUTES_IN_OVEN = 40
  TIME_PER_LAYER = 2

  def remaining_minutes_in_oven(actual_minutes_in_oven)
    remaining_minutes_in_oven = EXPECTED_MINUTES_IN_OVEN - actual_minutes_in_oven
    return remaining_minutes_in_oven
    # raise 'Please implement the Lasagna#remaining_minutes_in_oven method'
  end

  def preparation_time_in_minutes(layers)
    preparation_time_in_minutes = layers * TIME_PER_LAYER
    return preparation_time_in_minutes
    #raise 'Please implement the Lasagna#preparation_time_in_minutes method'
  end

  def total_time_in_minutes(number_of_layers:, actual_minutes_in_oven:)
    return (number_of_layers * TIME_PER_LAYER) + actual_minutes_in_oven
    #raise 'Please implement the Lasagna#total_time_in_minutes method'
  end
end

fatcat = Lasagna.new
# p fatcat.remaining_minutes_in_oven(10)
# p fatcat.preparation_time_in_minutes(3)
#p fatcat.total_time_in_minutes()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

🌚 Life is too short to browse without dark mode