Ruby Koans
Throughout the Ruby section of this course, we will assign you exercises to complete, which reinforces the new knowledge you learn. The exercises are packaged in a program called Ruby Koan. Ruby Koan is a collection of tests covering all the fundamental concepts of Ruby. We will write in the schedule the tests we expect you to complete for the week.
During the exercises, you might need to Google around to learn something new on your own to solve some of the challenges. Read on for instructions to install and run Ruby Koans.
- Download Ruby Koan. Unzip files (preferably to a directory convenient for accessing via Terminal).
- Go to Terminal and try running
$ rake
rake
stands for ruby make, which is to run ruby codes
AboutAsserts#test_assert_truth has damaged your karma.
The Master says:
You have not yet reached enlightenment.
Do not lose hope.
The answers you seek...
(select text below to see answer)
|||||||||||||||||||||
Please meditate on the following code:
.../ruby_koans/about_asserts.rb:10:in `test_assert_truth'
mountains are merely mountains
your path thus far [X_________________________________________________] 0/168
What $ rake
does is that it will run through all the challenges in Ruby Koans. The messages tell you that you have a score of 0/168
, which means that there are 168 challenges, and you haven't passed any challenge yet.
It also tells you the immediate challenge is in about_asserts.rb
file. Ruby Koans will guide you through all the challenges through these messages. When you have solved a challenge and want to see if you've done it correctly, just run $ rake
again.
The answer to each question is masked on purpose. Please only use it after 10 minutes of trying. You have to select to highlight the answer in order to see it.
To solve problems, open the file and edit your answers into the file.
For example, in about_array_assignment.rb
:
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutArrayAssignment < Neo::Koan
def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal __, names
end
...
end
The challenge is trying to assert that names
is indeed equal to ["John", "Smith"]
, so you should do:
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutArrayAssignment < Neo::Koan
def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal ["John", "Smith"], names
end
...
end
Now, run the test again $ rake
, and you should get 1/168
.
Tips: You can make
rake
run automatically every time you change any test files, so you have to do it type it out manually every time. Install Observr$ gem install observr
and run$ observr koans.watchr
. Now, whenever you change a file and save, it will automatically runrake
. To quit, type ctrl + c.
Always try to solve the problem yourself, instead of looking at "The answers you seek..." for solutions. You don't learn anything if you don't try to think hard and solve them on your own.
References
Files | Purpose |
---|---|
about_asserts.rb |
Teach you what the assert method do |
about_strings.rb |
double quotes; single quotes; backslash; flexible quotes; EOS; concatenate; += vs <<; escape chars; interpolate #{}; substring; .split; .join; strings are unique objects |
about_symbols.rb |
compare symbols; methods of symbol; string to symbol; symbols are not strings; purpose of symbols |
about_true_and_false.rb |
truthy and falsey |
about_methods.rb |
global method; calling without parentheses; arguments; default arguments; args as a var; explicit vs implicit return; class methods; public methods; private methods |
about_control_statements.rb |
if, then statements; unless statement; break; next; .times; for item in array; |
about_array.rb |
arrays; accessing; slicing; ranges; push & pop; |
about_array_assignment.rb |
playing with variable assignments with arrays |
about_iteration.rb |
each; collect; map; select; find_all; find; inject |
about_hashes.rb |
create; access; fetch; modify, hash is unordered; .keys; .values; default values; array has default value; mutable vs immutable |
about_object.rb |
everything is an object; to_s; inspect; object id; clone object |
about_nil.rb |
.nil? , .inspect , .to_s ; error; error messages; |
about_keyword_arguments.rb |
default arguments |
about_scoring_project.rb |
practice on basic programming |
about_classes.rb |
.class on custom Class; set_name; instance variable @variable; attr_reader; attr_accessor; def initialize; "STRING".inspect |
about_inheritance.rb |
ancestors; inherit; super |
about_modules.rb |
classes override module methods; modules can't instantiate |
about_dice_project.rb |
practice on Class |