Skip to content

Instantly share code, notes, and snippets.

@M0119
Last active March 19, 2019 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save M0119/cae92df2f7c8ba01c8de9eb7186359f1 to your computer and use it in GitHub Desktop.
Save M0119/cae92df2f7c8ba01c8de9eb7186359f1 to your computer and use it in GitHub Desktop.

Lecture ✍️

One liners

There are many different ways in ruby to write the same thing.

puts (if 1 then 2 else 3 end)
# => 2
puts 1 ? 2 : 3
# => 2
x = if 1 then 2 else 3 end
puts x
# => 2

This is a ternary operator. It's a different syntax for control flow.

result = true ? "true" : "false"
puts(result)
print ("What is your name? ")
name = gets().strip
name = name.length > 20 ? name.slice(0, 20) : name
puts(name)

Comparing loops

When you want to do the same thing a fixed amount of times.

50.times { print("=") }
50.times do 
    print("=") 
end

When you want to do something to each element of an array.

arr.each do |item|
    puts (item)
end

When you want to do something to each element of an array and use the index.

arr.each_with_index do |item, index|
    puts ("#{index}. #{item}")
end

When you don't have a fixed number of interations.

while (retry_count < PASSWORD_RETRY_LIMIT) && (!is_valid_password(password))
    password = get_password()
end

File handling

Ruby comes with file handling. No gem required.

To display the lines in a file.

File.open("todo-list.txt").each do |line|
    puts line
end

To append to a file.

File.open("todo-list.txt", "a") do |line|
    line << "Buy milk\n"
    line << "Put out garbage\n"
    line << "Do laundry\n"
end

To overwrite a file.

File.open("todo-list.txt", "w") do |line|
    line << "Buy milk\n"
    line << "Put out garbage\n"
    line << "Do laundry\n"
end

This is an example of reading in a CSV file, converting the rows to hashes and then pushing that data into an array.

require 'csv'    
require 'pry'
csv_text = File.read('world-cities.csv')
csv = CSV.parse(csv_text, :headers => true)
cities = []
csv.each do |row|
    row_data = row.to_hash
    if ( row_data['country'] == 'Australia' && row_data['subcountry'] == 'Victoria')
        puts row_data
        cities << row_data
    end 
end
puts cities.length

References

# Create a constant for the todo list file name
# Change the code to use the constant
# Write a method to display the to do list, pass the file name as a parameter
# Ask the user "what do they want to add to the to do list"
# Read each item in the to do list into an array
# Display each item in the array in a numbered list
# eg: 1. Put out garbage
# Write a method call load_cities that takes the CSV file name as a parameter and returns an
# array of cities
# Write a method called subcountry_city_count that takes three parameters; cities, country and subcountry,
# and returns the number of cities
# Call the subcountry_city_count method five times. Each time with a different set of paramters
# Write a method that takes the array of cities as a parameter and returns an array of coubntries
# Optional
# Write code to find out which subcountry has the most cities
# Write code to find out which country has the most cities
# Use the world cities CSV file with the Countries and Cities classes you created
# in a previous challenge.
# Extend the to do list b y making it a class and by adding
# - a menu with option add, delete, update, display
# - write methods to implement functionality for each of the menu items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment