🔧 Programmierung 🕛 vor 1 Jahr 5 Min Lesezeit
0

Water Intake Program in Ruby: Part 2

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

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.




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







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.




CODE
 def initialize(name, weight, minutes_exercised, temperature, pregnant)
@name = name
@weight = weight
@minutes_exercised = minutes_exercised
@temperature = temperature
@pregnant = pregnant
end






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:




CODE
  def calculate_weight_intake
@weight * 0.5
end

def calculate_exercise_intake
(@minutes_exercised / 30.0) * 12
end






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).




CODE
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






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.




CODE
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






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:




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

daily_intake = calculator.calculate_total_intake
glasses_of_water = calculator.glasses_of_water






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.




CODE
 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






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!

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
6 Quellen
CVE-2022-44255 | TOTOLINK LR350 9.3.5u.6369_B20220309 buffer overflow (EUVD-2022-47204)
2 Quellen
CVE-2026-68426 | Linux Kernel up to 6.18.41/7.1.5/7.2-rc3 xfrm validate_xmit_skb_list use after free (Nessus ID 346426)
1 Quelle
Windows 11 Probleme mit gültiger Domänenanmeldung nach September-Update [Workaround]
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Water Intake Program in Ruby: Part 2

Thematisch verwandte Begriffe: Water, Intake, Program, Ruby · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...