Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungFirst-touch attribution on a cookieless static Nuxt site(21.09.2026 um 02:51 Uhr)
Sichere ProgrammierungWho Is the Customer? It Might Not Be Who Uses the Product(21.09.2026 um 02:57 Uhr)
Sichere ProgrammierungOn My Japanese Team, We Greet Each Other by Saying "You Must Be Tired"(21.09.2026 um 03:06 Uhr)
Sichere ProgrammierungRedis vs Memcached: Complete Comparison(21.09.2026 um 03:16 Uhr)
Sichere ProgrammierungHow Databricks Serverless Compute Cost My Team $14k in One Weekend(21.09.2026 um 03:20 Uhr)
Sichere ProgrammierungStop trying to make Airflow work for Medallion pipelines(21.09.2026 um 03:21 Uhr)
Sichere ProgrammierungI built an app that turns workout videos into actual workouts(21.09.2026 um 03:39 Uhr)
Sichere ProgrammierungFirst-touch attribution on a cookieless static Nuxt site(21.09.2026 um 02:51 Uhr)
Sichere ProgrammierungWho Is the Customer? It Might Not Be Who Uses the Product(21.09.2026 um 02:57 Uhr)
Sichere ProgrammierungOn My Japanese Team, We Greet Each Other by Saying "You Must Be Tired"(21.09.2026 um 03:06 Uhr)
Sichere ProgrammierungRedis vs Memcached: Complete Comparison(21.09.2026 um 03:16 Uhr)
Sichere ProgrammierungHow Databricks Serverless Compute Cost My Team $14k in One Weekend(21.09.2026 um 03:20 Uhr)
Sichere ProgrammierungStop trying to make Airflow work for Medallion pipelines(21.09.2026 um 03:21 Uhr)
Sichere ProgrammierungI built an app that turns workout videos into actual workouts(21.09.2026 um 03:39 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Water Intake Program in Ruby: Part 2

Reagiere als Erste:r — dein Feedback zählt!

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

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

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

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

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

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

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

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!

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

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

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93974 | A flaw has been found in SourceCodester Online Reviewer Management Syste…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick