DEV Community

G Hewitt
G Hewitt

Posted on • Updated on

Water Intake Program in Ruby

To avoid getting stuck in tutorial hell, I decided to create a few small projects to apply the Ruby skills that I'm currently learning and record my progress on my blog.

After spending too many hours wondering what my first official project should be, I decided to work on a water intake program.

The goal of this program is to calculate how many ounces of water a person should drink daily based on their:

  • Weight

  • Daily Exercise

  • Special Circumstances (Pregnant/Breastfeeding, Outdoor temperature, etc.)

After calculating the required ounces of water, I will then tell the program to translate that into glasses of water, where 8 Ounces = 1 Glass.

Water Intake Research

The sources I used to research the calculations for this program are:

University of Missouri System

Kinetico Water Systems

According to both sources, The first thing to calculate when finding out your daily water intake is your weight. The formula for that is:

weight * 0.5 = ounces of water per day

The next step is to find out how many ounces to add to the previous result based on the amount of time you spend exercising that day. It is recommended to add 12 ounces every 30 minutes that you spend exercising. The formula would look like:

previous result + (minutes of exercise / 30 * 12) = ounces of water per day

Lastly, There are special situations where more ounces might be added to the total intake. For example, If someone is pregnant or breastfeeding, they will need to increase their intake by 32 ounces.

The temperature also plays a role in water consumption. In this program, If the current temperature is over 70 degrees, another 16 ounces will be added to the total daily intake.

Creating Program

Since each user will have their own results, I decided to make the program more personal by starting off with asking for their name. To do this, I used the method 'puts' followed by the "Enter your name:" in quotations. doing so will print the string to the console.

Afterwards, the console needs to read input from the the user. That is done by using the 'gets.chomp' the first of these methods, 'gets' allows the user to type their input, in this case, 'name', and turn it into a string. While 'chomp' is used to remove the new line character from the captured string. So far, the editor should look like this:

puts "Enter your name:"

name = gets.chomp
Enter fullscreen mode Exit fullscreen mode

From now on in the program, whatever the user input for 'name' string was, will show up anytime #{name} is present. This is called String Interpolation. After the user provides the program with their name, They will be introduced to text that explains how the program works, and ask if the user is pregnant.

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? If so, It is recommended to increase your daily intake by 32 ounces"

pregnant_response = gets.chomp
Enter fullscreen mode Exit fullscreen mode

The 'pregnant_response' string will look for either a 'yes' or 'no' response. This will play a part in the program later on. After the user enters a response, the program will set the 'daily_intake' integer to 0. Doing so allows the variable of 'daily_intake' to start at zero and lets the program track the intake from there.

The program will then ask the user to put in their weight and the amount of minutes that they exercised today. We'll use the same 'gets.chomp' that we used earlier when asked to type your name, but with the addition of '.to_f'. This converts the string into a floating-point number. Which is a number with a decimal point.

daily_intake = 0

puts "Next, enter your weight:"

weight = gets.chomp.to_f

weight_intake = weight.to_f * 0.5

puts "Please enter the amount of minutes exercised today."

minutes_exercised = gets.chomp.to_f

exercise_intake = (minutes_exercised.to_f / 30) * 12

daily_intake = exercise_intake + weight_intake
Enter fullscreen mode Exit fullscreen mode

You might have noticed the two expressions that are typed out under both 'get.chomp' strings. These expressions are what we use to calculate the value of the variable. In the case of the 'weight_intake' variable, we use 'weight' variable multiplied by 0.5. Then we calculate the 'exercise_intake' variable by dividing the 'minutes_exercised' variable by 30 and multiplying that answer by 12.

the final expression shown above takes the answer from both the 'weight_intake' and 'exercise_intake' and adds them together giving you the 'daily_intake' variable.

puts "Now let's take into consideration the weather. What is the current temperature?:"

temperature = gets.chomp.to_i


if pregnant_response == "yes" 
  daily_intake += 32
end

if temperature >= 70
  daily_intake += 16
end

glasses_of_water = daily_intake / 8

puts "Thank you #{name}. You water intake for today is #{daily_intake} ounces. That equates to
around #{glasses_of_water.to_i} glasses of water. Stay hydrated, my friend."
Enter fullscreen mode Exit fullscreen mode

This is where the special circumstances come into play. Earlier in the code, the user was asked if they were pregnant or breastfeeding. If the answered 'yes' to that question. 32 ounces gets added to the final 'daily_intake' number. If the user entered an integer that was greater than 70 when asked what the temperature was, 16 ounces is also added to the final 'daily_intake' number.

Our last expression in the code is used to convert the amount of water in ounces to glasses of water by dividing our final 'daily_intake' by 8.

You might notice that 'glasses_of_water' has a string of '.to_i' instead of '.to_f'. This will round the variable to the nearest whole number. instead of a number with a decimal.

Creating this water intake program was a great way to apply the Ruby skills that I have been learning. I'm excited to continue working on more projects like this and sharing my progress on the blog!

Top comments (2)

Collapse
 
king_triton profile image
King Triton

Wow, this is a fantastic project idea! I love how you've broken down the problem into manageable steps and provided detailed explanations for each part of the code. The inclusion of special circumstances like pregnancy, breastfeeding, and temperature is a thoughtful touch that makes the program even more useful. It's great to see you applying your Ruby skills in such a practical way. Keep up the awesome work, and I can't wait to see what you come up with next!

Collapse
 
ghewitt95 profile image
G Hewitt

Thank you so much for reading and for your thoughtful feedback! I'm thrilled that you found the project idea useful. I'm looking forward to creating more projects and continuing to improve my skills. I’m also excited to check out your blog and see what you’re working on.