Bootcamp Day 2

Development Bootcamp – Day 2: Basic Programming

Hi, Ruby! Nice to meet you ;)!

The second day of the bootcamp started of with installing Ruby, which unfortunately was not that easy..! Several people encountered difficulties during the install. It took almost an hour before everyone was ready and set to go. It took me quite some time as well, but I still had 20 minutes left to go through the first parts of the reader. An introduction to the “Ruby Basics” which are more or less the basics for every programming language but using the Ruby Syntax.

Some of todays Ruby Basics

# Variables
my_age  =  25       # Integer
price   =  3.99     # Decimal
my_name =  "Julian" # String

# Input / Output
puts "Hi, there"    # Shows: "Hi, there" and goes to the next line.
gets.chomp          # gets prompts the user for input
                    # .chomp removes the new line

# If / Else
if my_age <= 17             # if the my_age variable is smaller or equal to 18 
   puts "No beer for you!"  # show "No beer for you!"
else                        # otherwise 
   puts "Cheers!"           # show "Cheers!"
end

# Methods 
def say_hi(name)
    "Hi there #{name}!"
end

# Arrays & Hashes 
# An array is an ordered list of things: a, b, c, d
arr = [a, b, c, d]

# A hash is a collection of key/value pairs.
pizzas = [
        {:name => "Margherita" , :price => 8},
        {:name => "Funghi"     , :price => 10},
        {:name => "Tonno"      , :price => 13},
        {:name => "Salami"     , :price => 13.50}
    ]

After a morning of fiddling with the basics, things got more and more complex, although still doable. Some things that took me some trial and error were:

  • Passing a parameter coming from a different method to a method
  • Creating an array of hashes
  • Using the keys within a hash

The final assignment of the day was creating a program to order pizza. This is what I came up with by the end of the day. Could probably use some refactoring but at least it works!

def pizzas 
    [
        {:name => "Margherita", :price => 8},
        {:name => "Funghi", :price => 10},
        {:name => "Tonno", :price => 13},
        {:name => "Salami", :price => 13.50}
    ]
end

def show_menu
    for pizza in pizzas
        puts "#{pizza[:name]}: #{pizza[:price]}"
    end
end
    
def ask_name
    puts "What's your name?"
    gets.chomp.capitalize
end


def pizza_choice
    puts "Which pizza would you like? (Please select by name)"
    gets.chomp.capitalize
end


def number_of_pizzas 
    puts "How many pizzas do you want?"
    order_amount = gets.chomp.to_i
end


def print_total_costs(pizza_choice, number_of_pizzas, pizzas,name)
    
    puts "Thank you for you order #{name}!"
    puts "You ordered #{number_of_pizzas} pizza #{pizza_choice}."
    
    if "#{pizza_choice}" == "Margherita"
        total_cost = "#{pizzas[0][:price] * number_of_pizzas}".to_i
        if total_cost > 20
            total_cost *= 0.8
        elsif total_cost < 10
            total_cost += 2.5
        end
        puts "The total cost of your order is #{total_cost.round(2)}"
    elsif "#{pizza_choice}" == "Funghi"
        total_cost = "#{pizzas[1][:price] * number_of_pizzas}".to_i
        if total_cost > 20
            total_cost *= 0.8
        elsif total_cost < 10
            total_cost += 2.5
        end
        puts "The total cost of your order is #{total_cost.round(2)}"
    elsif "#{pizza_choice}" == "Tonno"
        total_cost = "#{pizzas[2][:price] * number_of_pizzas}".to_i
        if total_cost > 20
            total_cost *= 0.8
        elsif total_cost < 10
            total_cost += 2.5
        end
        puts "The total cost of your order is #{total_cost.round(2)}"
    elsif "#{pizza_choice}" == "Salami"
        total_cost = "#{pizzas[3][:price] * number_of_pizzas}".to_i
        if total_cost > 20
            total_cost *= 0.8
        elsif total_cost < 10
            total_cost += 2.5
        end
        puts "The total cost of your order is #{total_cost.round(2)}"
    end
  
    # Apply discounts
    # Print out
    
end

puts "Welcome to Julian's Pizza Place!"
name = ask_name
puts "Hi #{name}! Have a look at our menu."
show_menu

print_total_costs(pizza_choice, number_of_pizzas, pizzas,name)

An extra optional assignment was creating a program which tells you in what year you will get to the age of 100. My code:


def get_name
puts "What's your name?"
gets.chomp
end

def get_age
puts "How old are you?"
gets.chomp
end

def get_gender
puts "What's your gender (m/f)?"
gets.chomp
end

def calc_year(get_name, get_age, get_gender)
    
    if "#{get_gender}" == "m"
        puts "Good morning Mr. #{get_name}!"
    else
        puts "Good morning Ms. #{get_name}!"
    end
    
    time = Time.new
    
    year_hundred = time.year + (100 - "#{get_age}".to_i)
    
    puts "You will be 100 in the year: #{year_hundred}."

end

calc_year(get_name, get_age, get_gender)

It was a very intense day and it's unbelievable this was just the second day of the bootcamp! Already Learned so many new things which gets me super excited and I can't wait for tomorrow!

Sweet dreams and see you tomorrow Ruby ;)!

Bonus video: First thing that came up when I thought about Ruby!

Leave a reply:

Your email address will not be published.