Skip to content

Instantly share code, notes, and snippets.

View harrisonmalone's full-sized avatar

Harrison Malone harrisonmalone

View GitHub Profile

Morning ruby drills

The test in the morning was to get you drilled in the basics of ruby. Refer back to it and make sure you practice as often as you can!

Binary numbers and bases

Today we looked at binary numbers and the way in which they work in discrete maths. We also touched on different number systems such as base-2, base-10 and base-16. Base-10 has 10 digits (0,1,2,3,4,5,6,7,8,9) and base-2 has 2 digits (0,1). Base-16 has a weird alphabet thing going on. The following was my working out to some of the day's challenges.

Convert 11201 from base 3 to decimal
require "sqlite3"
db_file_path = 'posts_spec.db'
DB = SQLite3::Database.new(db_file_path)
DB.execute('DROP TABLE IF EXISTS `posts`;')
create_statement = "
CREATE TABLE `posts` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,

Post challenge

Sorry about the ambiguity for the morning challenge yesterday. Hopefully these instructions clear a few things up for people.

Instructions

Firstly, run the posts_spec.rb file to insert data into the posts_spec.db database. Use sqlite3 .schema to ensure you have a table in there.

# CREATE TABLE `posts`

Yield challenge

What is a block?

A block is simply a chunk of code, and yield allows you to "inject" that code at some place into a function. So if you want your function to work in a slightly different way where you don't have to write a whole new function,you can instead reuse a method you already have, but give it a different block.

Use cases for yield

Consider a function that will print the members of an Array, but number them, like this: if you give it ["apple","banana"], it should print

Rails Practice Steps

In Rails, you'll be running the same commands in terminal to generate different things (models, controllers etc). The idea is to practice by repeating this process for several different projects and we'll be doing this for most of today.

We would like you to rip off your favorite websites (GoodReads, IMDB, etc - something where you can mimic a simple version of their database). No need to overdo it - just start simple and go from there. This is about the process rather than creating an awesome web-app.

It’s also important to understand what we are doing in each step, so don’t just follow these blindly. Try to tie these back to a visual representation of Rails and MVC, and feel free to ask me about that if you need. There are many web tutorials that can help you to visualize the pattern.

The idea here is to run through these steps several times until you have the pattern down totally. It’s also designed to have you run into the errors. Please read the errors each time they arise (t

id team city premierships
1 collingwood melbourne 15
2 west coast perth 4
3 brisbane brisbane 3
4 gws sydney 0
5 north melbourne melbourne 4
6 carlton melbourne 16
7 sydney sydney 5
8 melbourne melbourne 12
9 western bulldogs melbourne 2
# board begins its life as an array of empty strings
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
# method for displaying the tictactoe board
def display_board(board)
puts " 1 2 3"
puts "A #{board[0]} | #{board[1]} | #{board[2]}"
puts " ----------"
puts "B #{board[3]} | #{board[4]} | #{board[5]}"
puts " ----------"
class Array
def my_map
new_arr = []
self.each do |elem|
new_arr << yield(elem)
end
return new_arr
end
def my_select
numbers = "2 4 7 8 10"
def iq_test(numbers)
#your code here
numbers_array = numbers.split(" ")
numbers_int = numbers_array.map! do |number|
number.to_i
end
odd_arr = []
even_arr = []

Allergies challenge

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)