master
Thom Page 10 years ago
parent ecb66c7f22
commit 580471221f

@ -0,0 +1,146 @@
# RUBY AND SINATRA PRACTISE
![rs](https://thejewelerblog.files.wordpress.com/2014/07/guysanddolls1.jpg)
Make a homework.rb file in your repo for tonight and put the answers
to Part One in there.
## PART ONE: METHODS AND CLASSES
### 1. Palindrome
Make a ruby method that returns true if a string is a palindrome, false if not. Make sure it returns the correct result for words with capital letters.
### 2. The Caesar Cipher
From Wikipedia:
> In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. The method is named after Julius Caesar, who used it in his private correspondence.
We're going to implement a simple Caesar Cipher called ROT13 ("rotate by 13 places"). The transformation can be represented by aligning two alphabets, like so:
```
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
```
ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption. ROT13 is used in online forums as a means of hiding spoilers, punchlines, and puzzle solutions from the casual glance.
**Directions:**
Read up on the difference between **class methods** and **instance methods** here: (http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/). Just read through the first two examples, no need to dwell.
Make a class called `Cipher`, and implement a **class method** `encode` that takes a word as an argument and returns its ciphertext using ROT13.
You can then call the class and its method and get a result as follows:
```
Cipher.encode("hello")
=> "uryyb"
Cipher.encode("peter")
=> "crgre"
```
### 3. The Universe
Make a class called `Universe` that:
1. Takes three parameters. These are the three things within the universe. Save these things to instance variables using an `initialize` method.
2. Has an instance variable `@expanding = true`
3. Has an instance variable `@conservation = true`
4. Has a method that will print out all the things in this universe.
5. Has a method called `create` that takes a parameter and will add a new thing to the universe (taken from the parameter). If `conservation = true` then one of the three things in the universe is *replaced* by the new thing. If not, then a new thing is added to the others.
6. Has a method that changes `expanding` to `false` if there are more than ten things in the universe, which means the universe is now contracting . . . (there's no stable universe in this scenario).
7. Has a method called `whim` that changes `conservation` from `true` to `false`
8. Has a method called `crunch` wherein, if `expanding` equals `false`, removes each item from the universe (sets it to null) and prints a string saying the name of the
item and that it has been crunched due to gravity. When the final item remains, remove that item and print "The Universe has ended."
Test out your Universe by instantiating a `new` Universe with its parameters and running your commands.
-----------
## PART TWO: SINATRA
### Sinatra Grocery Store II
Build a Grocery Store App with Sinatra. Using parameters, the app should simply return the cost of the grocery in question. Remember to quit and reload your app when you make changes! Remember that ctrl + C quits out of your server! Remember to load your server at localhost:4567 in your browser to see the result!
Examples:
| Sample Path | Info On Page |
| ----------- | ------------ |
| /store/lettuce | Lettuce costs: $7 |
| /store/gruyere | Gruyere costs: $110 |
| /store/mussels | Mussels costs: $41 |
| /store/plantains | Plantains costs: $5 |
| /store/cashews | Cashews costs: $20 |
| /store/icecream | Icecream costs: $9 |
| /store/knish | Knish costs: $3 |
| /store/baklava | Baklava costs: $2.5 |
### Setup
Make a `store.rb` file. Remember to `require sinatra` at the top of your `store.rb` file.
### Part 1:
Start by preparing `store.rb` and getting the eight routes to work with the correct message as in the table above.
### Part 2:
Using this as a clue:
```
get '/hello/:name/' do
"Why, hello #{params[:name]}"
end
```
Make it so you can choose what the price of your groceries will be by entering it into the URL bar.
For example `store/lettuce/7` will show: `Lettuce costs: $7`
### Part 3:
Using this as a clue:
```
get '/:name/:city' do
"Why, hello #{params[:name]} from #{params[:city]}"
end
```
Make it so you can enter any grocery that could possibly exist, and any price, and get the result as above.
### Part 4:
1. Make a route that redirects `'/'` to `'/store'`.
2. Make a 'not found' message that appears if the route is not found (404).
#### End

@ -0,0 +1,108 @@
# STARSHIP FLEET Sinatra app
![umbra](http://41.media.tumblr.com/197ffa196198782c06e55926c6b7648d/tumblr_n1n8rxfPIn1rm3djoo1_500.jpg)
You are the manager for a fleet of alien starships, and the **Great Void Face** has demanded that you make an app to manage the starship inventory. You cannot refuse the **Great Void Face**. You really want to make an impression. Create an app that will manage all the starships and aliens in the fleet, and have the capability to Cread, Read, Update, and Destroy both starship and alien entries.
#####
## Step 0 - SETUP FILES AND DATABASE
* Make a folder for your app
* Set up your Gemfile
* Touch a server.rb for your controllers
* Set up your config.ru (make sure it requires server.rb and will run a class called AppController)
* `bundle install` to install all the gems
* Make a database called `starships_app`
## STEP 1 - DATABASE SCHEMA
**MODELS:**
* A starship has many aliens
* An alien belongs to a starship
* Remember, foreign keys always go in the many, in this case there are many aliens for a starship.
* Make a schema.sql file and in it make tables for the following:
#### STARSHIPS TABLE
* serial primary key - `id`
* varchar(255) - `name`
* text - `img_url`
* varchar(255) - `description`
* varchar(255) - `star_system`
#### ALIENS TABLE
* serial primary key - `id`
* varchar(255) - `name`
* text - `img_url`
* varchar(255) - `role`
* integer - `starship_id` (this is the foreign key)
## STEP 2 - DATABASE SEED
Seed your database initially with INSERTs for two starships. The star_system can be real or invented. For the image_url you can just take an image url from the widespread service known as the internet.
Seed your database with INSERTs for two aliens per starship. Each of the two alien's roles per starship should be 'captain' and 'engineer' to begin with. Remember to set the correct foreign keys (one captain per ship).
* Check your database to make sure it's all good
## STEP 3 - ROUTES AND CONTROLLERS
#### AppController class
* should have all seven RESTful actions for starships
index route: `GET '/starships'` sends data for all starships
new route: `GET '/starships/new'` sends a form to make a new starship
create route: `POST '/starships'` creates the new starship, will redirect to the show route
show route: `GET '/starships/:id'` sends data for a single starship
edit route: `GET '/starships/:id/edit'` sends a form to edit a starship
update route: `PUT '/starships/:id'` updates the starship, will redirect to the show route
delete route: `DELETE 'starships/:id'` deletes a starship, will redirect to index
* start with a basic GET request and test from there
* should have all seven RESTful actions for aliens
* start with a basic GET request and test from there
## STEP 4 - VIEWS
#### ALIENS
* `alien_index.erb` - should display every alien in a seperate div, and the div should contain the `name`, `image`, `starship name` and the `role` of that alien. You should also have a link to the single alien for each one. The text displaying the starship name should link to the starship show page. Should also provide a link to create a new alien
* `alien_show.erb` - Should display the single alien with all its info, including the starship name. Should also provide a link to edit the alien
* `alien_new.erb` - Should display a form to create a new alien. The form should have a drop down of existing starships to select which the alien will belong to
* `alien_edit.erb` - Edit form should be identical to the new form but it should also have a delete button to destroy the alien
#### STARSHIPS
* `starship_index.erb` - should display every starship in a serperate div, the div should contain all of the starship information and its image. You should have a link that sends you directly to the starship show page. Should also provide a link to create a new starship.
* `starship_show.erb` - Should display the starship in a div, the div should contain all the starship's information. Should also provide a link to edit the starship. You should also display all the starship's aliens in seperate divs, these divs should contain all the information for each alien and link to the specific alien
* `starship_new.erb` - Should display a form to create a new starship.
* `starship_edit.erb` - Should display a form to edit a starship, the edit form should provide a button to delete the starship.
## STEP 5
* Style your page using static public files from the public folder (css, images, whatever you want to explore)
* Expand your alien fleet to please the **Great Void Face**.
## BONUS
* Make it so that when you delete a starship, all of the aliens associated with that starship are also deleted.
---

@ -0,0 +1,84 @@
![ga](http://mobbook.generalassemb.ly/ga_cog.png)
# WDI-PANTHALASSA
---
Title: Memeory Lane - Part 1<br>
Type: Homework<br>
Duration: One evening<br>
Creator: Thom Page <br>
Course: WDIr-Panthalassa<br>
Competencies: Basic Ruby<br>
---
# MEMORY LANE - Part 1
Remember all the morning exercises we did in Javascript? Let's revisit some of these exercises one-by-one. We already have an idea how to approach them having done them before, so let's try to solve them again while also translating them to Ruby.
### 1. EULER PROBLEM 1
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
<hr>
** Commit your work.** <br>
The commit message should read: <br>
"Commit 1: Euler problem 1".
<hr>
### 2. PALINDROME
Make a ruby method that returns true if a string is a palindrome, false if not. Make sure it returns the correct result for words with capital letters.
<hr>
** Commit your work.** <br>
The commit message should read: <br>
"Commit 2: Palindrome".
<hr>
### 3. PRIMES
* Write a method called `prime?` that will test whether a number is Prime. The method will return true if Prime, false if not.
* Write another method called `primes_list` that will print all the Primes up to an arbitrary limit. For example, if you invoke your method with `primes_list(100)`, it will print all the Prime numbers up to and including 100.
This method can call on the previous `prime?` method.
```
A Prime number is a number that is not evenly divisible by another number except 1 and itself.
To test whether a number is Prime, you only need to test as far as the **square root** of that number. This
is advisable for optimization and testing large numbers.
```
Check out Ruby's `Prime` class: http://ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html
<hr>
** Commit your work.** <br>
The commit message should read: <br>
"Commit 3: Primes".
<hr>
### 4. BUBBLE SORT
Below are the Javascript instructions. Write your answer in Ruby:
https://github.com/ga-students/wdi-remote/blob/master/unit_01/w02d03/morning_exercise/bubble_sort.md
<hr>
** Commit your work.** <br>
The commit message should read: <br>
"Commit 4: Bubble Sort".
<hr>
### 5. BONUS: RECURSIVE FACTORIAL
Below are the Javascript instructions. Write your answer in Ruby:
https://github.com/ga-students/wdi-remote/blob/master/unit_01/w03d03/morning_exercise/recursive_factorial.md
<hr>
** Commit your work.** <br>
The commit message should read: <br>
"Commit 5: Recursive factorial".
<hr>

@ -0,0 +1,43 @@
# EULER1
result = (1...10).select do |num|
num % 3 == 0 || num % 5 == 0
end.reduce(:+)
# PALINDROME
def palindrome(word)
word == word.downcase.reverse
end
# BUBBLE SORT
def bubblesort(arr)
swap = true
while swap do
swap = false
(arr.length - 1).times do |i|
if arr[i] > arr[i + 1]
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swap = true
end
end
end
arr
end
# PRIMES
require 'prime'
def sum_primes(num)
sum = 0
Prime.each(num) do |n|
sum += n
end
sum
end

@ -0,0 +1,20 @@
# MEMORY LANE - Part 2
Remember all the morning exercises we did in Javascript? Let's revisit these exercises one-by-one. We already have an idea how to approach them having done them before, so let's try to solve them again while also translating them to Ruby.
### 6. WORD FREQUENCY
https://github.com/ga-students/WDI_NYC_Meeseeks/blob/master/unit_02/w05d01/morning_exercise/word_frequency.md
### 7. 100 GOBLINS
NOTE: Using `for` loops is not really the Ruby way. Look up the following looping methods: `.times`, `.step`, and `.each_with_index`.
https://github.com/ga-students/WDI_NYC_Meeseeks/blob/master/unit_02/w05d03/morning_exercise/goblins.md
### 8. PANDIGITAL NUMBERS
https://github.com/ga-students/WDI_NYC_Meeseeks/blob/master/unit_02/w05d04/morning_exercise/pandigital_numbers.md
### 9. LUHN ALGORITHM
https://github.com/ga-students/WDI_NYC_Meeseeks/blob/master/unit_03/w08d01/morning_exercise/luhn_algorithm.md
### 10. CLOCK HANDS
https://github.com/ga-students/WDI_NYC_Meeseeks/blob/master/unit_03/w08d05/morning_exercise/clockhands.md

@ -0,0 +1,52 @@
## RUBY CLASSES
#1. Classes
Watch the short video [here](https://www.youtube.com/watch?v=r6wVziWXYWI). 6 mins 23 seconds.
##Exercises:
1. Create a Dog class that takes in a name and puts `"Meet #{name}, your new dog!"` on `initialize`.
- Assign the dog a random fur color on initialize
2. Create a Bird class that takes in an adjective and a name and puts `"#{name} is a(n) #{adjective} bird!"` on `initialize`.
- Assign the bird a species at random on initialize
#2. Instance Methods
Watch the short video here [here](https://www.youtube.com/watch?v=c2a2bZf3LH4). 8 mins 22 seconds.
##Exercises:
1. Add instance methods to your dog
- Add a `bark` instance method to your dog that puts `"#{name} is barking!"`
- Add a `growl` instance mthods that puts "Grrrrrrr..."
3. Add instance methods to your bird
- Add a `hungry` method that puts `"#{name} wants a cracker!"`
- Add a `fly` methods that puts `"Flap! #{name} is taking flight!"`
4. Make your bird's ajective and name accessible
#3. Class Methods and 'Self'
Watch the short video [here](https://www.youtube.com/watch?v=0r93ZhzpeqI). 8 mins 28 seconds.
##Exercises:
1. Experiment with `self`
- Add a class method `self.what_is_self` to your Bird class and have it put `self`.
- Add an instance method `what_is_self` to your Bird class and have it put `self`.
- Try calling both of these methods in Pry and see what they return.
2. Add a class method to make puppies!
- add an instance variable of quality to your dog (here we will put a characteristic like 'playful', 'brave', 'loyal')
- add a class method of `make_puppy` to your dog class that takes in two dogs and returns a new dog.
- the new dog should have fur color of the first dog and the quality of the second dog.
- make puppies!
#4. Inheritance
Watch the short video [here](https://www.youtube.com/watch?v=BJWcH8Pnafw). 6 mins 56 seconds.
##Exercises:
1. Create a class called `Dessert` that has instance variables of `@name` and `@sugar_content`
2. Give it an instance method of `eat` that puts `"Yum! This #{name} is sooooo delicious!"`
3. Make two new classes called `Pie` and `DeepFriedDessert` that inherit from `Dessert`
4. Give the `DeepFriedDessert` its own `eat` method that instead puts `"Yum! This #{name} is sooo...ack! ugh! *heart-attack*"`
5. Make a new class `IceCream` that inherits from `Dessert` use `super` to get the instance variables of `@name` and `@sugar-content`. Also give `IceCream` its own unique instance variable of `@toppings`

@ -0,0 +1,146 @@
# RUBY AND SINATRA PRACTISE
![rs](https://thejewelerblog.files.wordpress.com/2014/07/guysanddolls1.jpg)
Make a homework.rb file in your repo for tonight and put the answers
to Part One in there.
## PART ONE: METHODS AND CLASSES
### 1. Palindrome
Make a ruby method that returns true if a string is a palindrome, false if not. Make sure it returns the correct result for words with capital letters.
### 2. The Caesar Cipher
From Wikipedia:
> In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. The method is named after Julius Caesar, who used it in his private correspondence.
We're going to implement a simple Caesar Cipher called ROT13 ("rotate by 13 places"). The transformation can be represented by aligning two alphabets, like so:
```
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
```
ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption. ROT13 is used in online forums as a means of hiding spoilers, punchlines, and puzzle solutions from the casual glance.
**Directions:**
Read up on the difference between **class methods** and **instance methods** here: (http://www.railstips.org/blog/archives/2009/05/11/class-and-instance-methods-in-ruby/). Just read through the first two examples, no need to dwell.
Make a class called `Cipher`, and implement a **class method** `encode` that takes a word as an argument and returns its ciphertext using ROT13.
You can then call the class and its method and get a result as follows:
```
Cipher.encode("hello")
=> "uryyb"
Cipher.encode("peter")
=> "crgre"
```
### 3. The Universe
Make a class called `Universe` that:
1. Takes three parameters. These are the three things within the universe. Save these things to instance variables using an `initialize` method.
2. Has an instance variable `@expanding = true`
3. Has an instance variable `@conservation = true`
4. Has a method that will print out all the things in this universe.
5. Has a method called `create` that takes a parameter and will add a new thing to the universe (taken from the parameter). If `conservation = true` then one of the three things in the universe is *replaced* by the new thing. If not, then a new thing is added to the others.
6. Has a method that changes `expanding` to `false` if there are more than ten things in the universe, which means the universe is now contracting . . . (there's no stable universe in this scenario).
7. Has a method called `whim` that changes `conservation` from `true` to `false`
8. Has a method called `crunch` wherein, if `expanding` equals `false`, removes each item from the universe (sets it to null) and prints a string saying the name of the
item and that it has been crunched due to gravity. When the final item remains, remove that item and print "The Universe has ended."
Test out your Universe by instantiating a `new` Universe with its parameters and running your commands.
-----------
## PART TWO: SINATRA
### Sinatra Grocery Store II
Build a Grocery Store App with Sinatra. Using parameters, the app should simply return the cost of the grocery in question. Remember to quit and reload your app when you make changes! Remember that ctrl + C quits out of your server! Remember to load your server at localhost:4567 in your browser to see the result!
Examples:
| Sample Path | Info On Page |
| ----------- | ------------ |
| /store/lettuce | Lettuce costs: $7 |
| /store/gruyere | Gruyere costs: $110 |
| /store/mussels | Mussels costs: $41 |
| /store/plantains | Plantains costs: $5 |
| /store/cashews | Cashews costs: $20 |
| /store/icecream | Icecream costs: $9 |
| /store/knish | Knish costs: $3 |
| /store/baklava | Baklava costs: $2.5 |
### Setup
Make a `store.rb` file. Remember to `require sinatra` at the top of your `store.rb` file.
### Part 1:
Start by preparing `store.rb` and getting the eight routes to work with the correct message as in the table above.
### Part 2:
Using this as a clue:
```
get '/hello/:name/' do
"Why, hello #{params[:name]}"
end
```
Make it so you can choose what the price of your groceries will be by entering it into the URL bar.
For example `store/lettuce/7` will show: `Lettuce costs: $7`
### Part 3:
Using this as a clue:
```
get '/:name/:city' do
"Why, hello #{params[:name]} from #{params[:city]}"
end
```
Make it so you can enter any grocery that could possibly exist, and any price, and get the result as above.
### Part 4:
1. Make a route that redirects `'/'` to `'/store'`.
2. Make a 'not found' message that appears if the route is not found (404).
#### End
Loading…
Cancel
Save