https://my.schooler.biz/s/1765/ruby/1/
https://www.dropbox.com/sh/37pn4k9ng6xl1bx/AABlHYaOvcIBvcjC5pObgfs3a?dl=0
https://www.dropbox.com/sh/9aahdthehvsit9u/AABwkdoMiHoFXdRoe2faIixRa?dl=0
- Show ruby syntax: variables, operators, first programs, loops, blocks, functions, conditions, exceptions.
- Lab - sum of digits in user input
puts sum_digits(123) # prints 6
puts sum_digits(39) # prints 12
- Lab - write a program that calculates factorial(n)
puts fact(5) # prints 120 (because 5 * 4 * 3 * 2 * 1 == 120)
- Lab - find net amount in bank account based on transaction log (D 200, W 50 => 150)
log = <<END
D 200
D 100
D 50
W 80
END
puts net_amount(log) # print 270 (200 + 100 + 50 - 80)
- Lab - write a function that takes a string and decides if that string is a pelindrom (i.e. reads the same from start to end and from end to start)
pelindrom?('abba') # true
pelindrom?('dog') # false
- Data types: Array, Hash, String, Integer, Float, symbols
- Lab - write a function that takes a list of numbers and returns only the ones > 10
numbers = [10, 20, 30, 1, 5, 99, 2]
puts larger_than_ten(numbers).inspect # prints: [20, 30, 99]
- Lab - write a function that takes a string and returns it in reverse words order
text = 'I can see the mountain'
puts reverse_words(text) # prints 'mountain the see can I'
- Lab - find words that appears most often in text
text = <<END
A ruby is a pink to blood-red colored gemstone,
a variety of the mineral corundum (aluminium oxide).
Other varieties of gem-quality corundum are called sapphires.
Ruby is one of the traditional cardinal gems, together with amethyst, sapphire, emerald, and diamond.
The word ruby comes from ruber, Latin for red.
The color of a ruby is due to the element chromium.
END
# should print "The mots common word is "for" and it appears 4 times
puts most_common_word(text)
- Lab - Write a function that takes as input two file extensions, searches for all files with the given first extension and renames them to have the second extension. If passed :debug => true symbol it should only print the intended changes without actually changing anything
# Assume the folder has the files a.txt, b.txt and c.txt
rename_all('txt', 'old', debug: true)
# prints:
# a.txt => a.old
# b.txt => b.old
# c.txt => c.old
rename_all('txt', 'old') # Rename all the files without printing anything
- Write a class Point so the following code should work:
p1 = Point.new(10, 20)
p2 = Point.new(15, 25)
puts p1.x # print 10
puts p1.y # print 20
puts p1.left_to?(p2) # true, x goes from left to right
puts p1.below?(p2) # false, y goes from top to bottom
- Write a class called Accumulator so the following code should work:
acc = Accumulator.new
acc.add(10)
acc.add(10, 20)
acc.add(25)
puts acc.val # prints 65
- Write a class called Race and another called Car so the following should work:
race = Race.new
race << Car.new(color: 'red', speed: 25) << Car.new(color: 'blue', speed: 40)
race << Car.new(color: 'green', speed: 10)
puts race.fastest_car # prints '<Blue, 40>'
-
Write ruby classes for a game of Tic-Tac-Toe. The game should have a text based UI, and allow either single player mode (against the computer) or dual player mode. Example game here: https://playtictactoe.org/
Rails playlist: https://my.schooler.biz/s/1774/ror/1/
Topics: ApplicationController, helpers, ERB, Routes
Demo App
- Show two pages, one for "about us" and one for "contact"
- Put menu bar in layout
- Lab - Build a website that shows the list of files in a selected (hard-coded) directory:
-
The list should show F next to a regular file or D next to a directory name.
-
Add a "view" button next to each file, clicking it should show the file's contents
-
Add a "delete" button next to each entry, clicking it should delete the thing
-
Add a "duplicate" button next to each file, clicking it should duplicate the file.
-
Don't forget to write controller tests as you go
Demo App
- Modify the lab built by students so each entry is a partial
- Add login/logout pages, so list of files is only available to signed in users
- log each action by the user that performed it
- limit number of actions users can make via session (and show why it's not a good idea)
- Lab - Start building a shared give-and-take board with users
- Build a website with login and logout routes
- Create a "giving" page which shows all the items users want to give away, use hard coded values in the controller to represent the items
- Create a "requests" page which shows all the items users want to receive, use hard coded values in the controller to represent the items
- Both pages should use the same layout allowing users to login/logout
- Don't forget to write controller tests as you go
./bin/rails g model Provider name:string description:string
./bin/rake db:migrate
./bin/rake db:rollback
- Lab - continue building our give-and-take board
- Move all users to the database by creating a User model
- Add signup page which creates a new user
- Add an Admin user which has a different layout and can see all users in the system
- Show all users paginated
- Lab - Linking todo items and users
- Only logged in users can create items
- Each item is created with the current user's id
- Users can only edit/destroy their own items
- Lab - Admin Interface
-
Add special "admin" user with password: "admin"
-
User admin can see all existing users (User Index)
-
Admin can edit user details (User Edit Form)
-
In the edit form admin can change:
- user name
- existing todo items
- delete todo items
-
Bonus: Show how many todo items each user has
-
Testing