DEV Community

G Hewitt
G Hewitt

Posted on

Water Intake Program in Ruby: Part 2

After taking some time to familiarize myself with Ruby, I decided to take a look back at the Water Intake Program I created a few months earlier with the purpose of applying what I learned and (hopefully) simplify my code.

Creating the Program

The first major difference you will see in my code is that it starts with the addition of a class. The best way to describe a class is as a blueprint for creating objects, which makes sense because Ruby is an object-orientated language.

class WaterIntakeCalculator
  attr_accessor :name, :weight, :minutes_exercised, :temperature, :pregnant

Enter fullscreen mode Exit fullscreen mode

After naming the class, I added attributes by using attr_accessor. It is best described as a shortcut that creates methods for each attribute, so we don't have to define these methods individually.

Next, I created instance variables using "@" and put them inside the initialize method to set the starting values for each instance of my WaterIntakeCalculator class. This method should run every time a new object is created from the class.

 def initialize(name, weight, minutes_exercised, temperature, pregnant)
    @name = name
    @weight = weight
    @minutes_exercised = minutes_exercised
    @temperature = temperature
    @pregnant = pregnant
  end
Enter fullscreen mode Exit fullscreen mode

I had trouble understanding the concept of Instance Variables. It started to make more sense once someone explained it to me using this real world example:

Imagine kids sitting at the same table with their lunchboxes from home. Inside their lunchbox, they each have their own food and snacks that make it uniquely theirs. One child might have a turkey sandwich while someone else might have a fruit cup.

In this analogy, the lunchbox is the object I created from the class, the food inside it represents the instance variables, each holding specific values that belong only to their lunchbox.

Just like I did in the first version of this program, I made two methods, one to calculate weight intake and the other to calculate exercise intake. These will come in handy later:

  def calculate_weight_intake
    @weight * 0.5
  end

  def calculate_exercise_intake
    (@minutes_exercised / 30.0) * 12
  end
Enter fullscreen mode Exit fullscreen mode

To wrap it all together, I created a method that calculates the total water intake by combining the weight and exercise intake. This method will also adjust based on the needs of the user. Adding 32 extra ounces if your pregnant, and an additional 16 ounces if the current temperature is higher than 70°F. (In the future, I plan to integrate an API to automatically adjust intake based on real-time temperature.) Finally, I added a method that will convert the total intake into glasses of water (8 ounces each).

def calculate_total_intake
    intake = calculate_weight_intake + calculate_exercise_intake

    intake += 32 if @pregnant.downcase == 'yes'

    intake += 16 if @temperature >= 70

    intake
  end

  def glasses_of_water
    (calculate_total_intake / 8).to_i
  end 
end
Enter fullscreen mode Exit fullscreen mode

To complete the class above, I added the final 'end' to close the WaterIntakeCalculator class definition, while also making sure the code is properly structured and error-free.

def home
  puts "Enter your name:"
  name = gets.chomp

  puts "Hello #{name},
This is a water intake program that will estimate the amount of water you'll have to drink today. 
It will take into consideration:
 - Weight
 - Activity Level
 - Outdoor Temperature
 Let's get started! Are you currently pregnant or breastfeeding? (yes/no)"
 pregnant = gets.chomp

 puts "Next, enter your weight (in pounds):"
 weight = gets.chomp.to_f

 puts "Please enter the amount of minutes exercised today:"
 minutes_exercised = gets.chomp.to_f

 puts "Now let's take into consideration the weather. What is the current temperature?:"
 temperature = gets.chomp.to_i
Enter fullscreen mode Exit fullscreen mode

Everything above may look familiar if you read the first part of my Water Intake post. In the previous version, this script was scattered all across various sections. This time around, I organized it all under the method named 'home' (I couldn't think of a better name). Doing this made my code feel cleaner and easier to follow.

The next few lines following is where things get different:

calculator = WaterIntakeCalculator.new(name, weight, minutes_exercised, temperature, pregnant)

 daily_intake = calculator.calculate_total_intake
 glasses_of_water = calculator.glasses_of_water
Enter fullscreen mode Exit fullscreen mode

Here, I'm creating a new instance for the WaterIntakeCalculator class to hold the user's data for when the program calls for the 'calculate_total_intake' and 'glasses_of_water' methods.

 puts "Thank you #{name}. Your water intake for today is #{daily_intake} ounces. That equates to around #{glasses_of_water} glasses of water. Stay hydrated, my friend!"
end

puts home
Enter fullscreen mode Exit fullscreen mode

Finally, I closed the 'home' method and called it at the end of the file. When I run this program, it will execute everything within that method. Everything runs smoothly!

Conclusion

I’ve transformed my Water Intake Program by structuring it with a class and organizing my code into methods, making it cleaner and easier to understand. It’s exciting to see my growth in Ruby as I apply these concepts to enhance my projects. I look forward to continuing this journey and refining my skills further!

Top comments (0)