Monday, 20 March 2017

Learning Ruby

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. 

Friday, 3 March 2017

Review: February 2017

Hi all! 

February has been a pretty cool month. 

It kicked off with the Trinity Science Ball on 3rd February at the Ballsbridge Hotel. The gang scrubbed up pretty nicely, I think. 



On the 8th, I visited UCD to hold an antibiotic resistance education workshop with Dr. Bergin's science education Masters students. It was scary but mutually beneficial so that was cool and I'm glad I did it. If you're reading this, Shane, thanks for having me in (and Mags for helping set it up!). 




I had a really great Valentine's Day this year thanks to the wonderful Leon. We started dating on the 11th and saw The Space Between Us in Cineworld on Valentine's, then he made me dinner. I've had an amazing time with Leon in general this month, so have some selfies. 10/10. 



I spoke at Dublin Tech Summit on the 16th about youth in entrepreneurship. I was on a panel with Jordan Casey and Niamh Scanlon. The gang and Leon came out to support, which was cool. 




In other speaking news, Zeminar posted a video of some highlights from their 2016 event. You can spot me at 1:31. 




A LOT of time this month has been spent working on my book. I was approached by a publisher about writing a book through a sort of agency I'm in a few months ago, and March 1 was the deadline to send off the first three chapters so I had a lot to do. I had no idea writing a book was so hard but it's on something important to me, increasing citizen involvement with science, so it's quite rewarding. No deal has been agreed yet, so I can't say any more for now. Whatever happens, definitely a learning experience. 

College has continued to be intense, full of tough work but great fun with friends and the whole atmosphere. I just love being around college and hanging out with people and knowing there's always something on. It's so vibrant. I had loads of fun with the gang and lots of sleepovers this month as usual. I appreciate them so much. 

It's only a month to the European Youth Summit in Budapest I'm helping to organise as a council member of the Youth Platform of the European Talent Support Network, so we've been doing lots of work on that. I've also had meetings with the gang and college staff about the community labs idea. At the start of March, I thought of a name that I actually like and isn't taken: Lablinn. I like it because it's short, pretty easy to spell, not taken and relevant to the meaning ("lab" + "with us" in Irish). It's similar to GrĂ¡inne's idea of Saotharlinn (Irish word for lab but the last syllable replaced by "with us"), except pronounceable by non-Irish speakers. 

To end the month, the gang, Leon and I jetted off with DU Physoc to Edinburgh for a couple of days. We did a physics walking tour, visited an observatory and had good banter (and some sleep deprivation!). 



Honestly, there's probably stuff that I'm forgetting, but all in all it was a cool month. 

For March, I'll have a lot more time to do stuff I've been neglecting because of book deadlines, so I'm planning to do more for college, Lablinn, antibiotic resistance and perhaps reading and Ruby. 

Thanks for reading!