Hey guys!
I've been meaning to learn Ruby for years now, and finally did it this month! I started the Codecademy Ruby course a couple of weeks ago and finished yesterday, on the 19th. It felt great, because it was a perfect level of challenge so I was really learning but having a fun time, and when I tried it two years ago I only got 40% in. It's also really addictive -- I've been procrastinating by working on it, and it's been a bit all-consuming to the exclusion of my other things over the last few days (I did the last 53% of the Codecademy course over Saturday and Sunday).
I've discovered that I love Ruby. It's a beautiful language -- so high-level it reads almost like English and genuinely fun to write in (especially with the colour-coding Codecademy and lots of other things give you). It's not as fussy about syntax as other languages I've tried like Javascript and Python (very few semicolons required, whitespace irrelevant).
It's really intuitive and pretty forgiving -- it can do implicit returns in blocks, so you don't even actually have to type return(function).
It does require correct syntax, but the correct syntax is pretty simple and there are multiple ways to do things. Plus, I've finally started sort-of enjoying bugfixing -- I'm very impatient, but the payoff feels good. Shout-out to Leon for help with learning to notice them.
The syllabus featured basics like setting variables and doing calculations, looping (while, for, times), control flow (if/elsif/else, unless), booleans (true, false, AND, OR, NOT), arrays, hashes (sets of key-value pairs), blocks, sorting, symbols, improving code, procs, lambdas, classes and instantiation and more.
There were two projects I was proud of: one for a movie catalogue, and a simple bank simulator.
The movie catalogue allows you to add movies and their star-ratings, update their ratings, display the movies in the set and delete movies from the catalogue. I liked that because it was the first project I'd done that actually seemed like a real, potentially useful app. I learned some interesting things as well to customise it -- for example, variables can't have spaces in them, they have underscores instead, but users are going to input spaces as spaces so I added something in the code to replace spaces with underscores so the program could understand them. To make sure the program understood inputs no matter what case they were in, I downcased everything. A nice touch I added was displaying the movies remaining in the catalogue after a user deleted something.
Here's what it looks like when you delete Harry Potter from the catalogue:
There are also lots of other nice things, like it checking if a movie is already there when you try to add one, update or delete one and directing you accordingly.
Codecademy gave lots of hints and direction, but it was still really cool making and debugging it and coming up with ways to test it.
Here's the code:
movies = {
enders_game: 5,
zoom: 3,
harry_potter: 4
}
puts "What would you like to do?"
puts "--Type 'add' to add a movie"
puts "--Type 'update' to update a movie rating"
puts "--Type 'display' to see the movie ratings"
puts "--Type 'delete' to delete a movie"
choice = gets.chomp.downcase
case choice
when 'add'
puts "Type in the name of a movie"
title = gets.chomp.downcase
title.gsub!(/ /, "_")
if movies[title.to_sym].nil?
puts "Rate this movie with a number from 1-5"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
else
puts "This movie is already here fuck off"
end
when 'update'
puts "Type in the title"
title = gets.chomp.downcase
title.gsub!(/ /, "_")
if movies[title.to_sym].nil?
puts "This movie is not here"
else
puts "Give new rating from 1-5"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
end
when 'display'
movies.each do |movie, rating|
movie = movie.to_s
movie.gsub!(/_/," ")
movie = movie.capitalize
puts "#{movie}: #{rating}"
end
when 'delete'
puts "Type in the title you'd like to remove"
title = gets.chomp.downcase
title.gsub!(/ /, "_")
if movies[title.to_sym].nil?
puts "Error! Movie not found"
else
movies.delete(title.to_sym)
movies.each do |movie, rating|
movie = movie.to_s
movie.gsub!(/_/," ")
movie = movie.capitalize
puts "#{movie}: #{rating}"
end
end
else
puts "Error!"
end
Terribly sorry about the formatting, but there would've been a lot of screenshots otherwise.
My other favourite project was the banking app. That one sets up a checking account and lets you withdraw and deposit money and check your balance, if you have the right PIN. Codecademy gave more freedom on this one and it was really cool. I used private and public classes (e.g. private for pin and public for balance), which was a cool way to learn them. It was really simple to do the methods -- this project convinced me that I've learned a lot. Even though I don't have a clue of lots of things and haven't even thought about things like efficiency yet, I'm still proud of it.
Here's the code:
class Account
attr_reader :name
attr_reader :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
def pin_error
return "Access denied: incorrect PIN."
end
public
def display_balance(pin_number)
if pin_number == pin
puts "Balance: $#{@balance}."
else
puts pin_error
end
end
public
def withdraw(pin_number, amount)
if pin_number == pin
@balance -= amount
puts "Withdrew #{amount}. New balance: $#{@balance}."
else
puts pin_error
end
end
public
def deposit(pin_number, amount)
if pin_number == pin
@balance += amount
puts "Deposited #{amount}. New balance: $#{@balance}."
else
puts pin_error
end
end
end
checking_account = Account.new("Elle Loughran", 1800)
checking_account.deposit(1234, 200)
That results in "Deposited 200. New balance: $2000.".
Obviously, I have lots left to learn (and, uh, college and many other projects to work on...). I'm writing notes on Ruby now to remember syntax etc, then planning to do the Ruby on Rails course, maybe some HackerRank stuff, and then figure out what I can do on my own.
But this has been a lot of fun, and I'm really glad I'm doing it.
Monday, 20 March 2017
Subscribe to:
Post Comments (Atom)
-
This summer, I did an online pre-MBA with Harvard Business School called HBX CORe, mostly sponsored by the Naughton Foundation, who continue...
-
This is the sixth week of a series on this blog where I interview other climate and environmental activists. I hope these interviews help ...
-
This is the fifth week of a series on this blog where I interview other climate activists. I hope these interviews help connect climate ac...
No comments:
Post a Comment