Write code in each starter code file to pass the tests in the spec
folder!
This will be way more practice with Ruby methods and test driven development - and a great opportunity to explore some new tactics!
Fork this repo on GitHub.
git clone YOUR_FORK_CLONE_URL.git
cd ruby_method_drills
The files you'll work on are:
-
arguments.rb
-- (test file isargument_spec.rb
) -
strings.rb
-- (test file isstring_spec.rb
) -
iterators.rb
-- (test file isiterator_spec.rb
) -
numbers.rb
-- (test file isnumber_spec.rb
) -
hashes.rb
-- (test file ishash_spec.rb
)
We'll use a tool called rspec
to outline our objectives AND test our code as we go... hooray for Test Driven Development (TDD)!
rspec
is available as a Ruby gem, so start in your terminal by running the command:
gem install rspec
You might get an error here, but we believe in you. Fix it.
Take a look at one of the "spec" files in the spec/
folder. These are the tests that we want to make pass.
To run all the tests type the following in the command line:
rspec
# or run a specific spec file
rspec spec/argument_spec.rb
You should see this as part of your output:
Failures:
1) #say_hello returns 'hello'
Failure/Error: expect( say_hello ).to eq "hello"
NameError:
undefined local variable or method `say_hello' for #<RSpec::ExampleGroups::SayHello:0x007f9ef3a9cc68>
# ./spec/argument_spec.rb:14:in `block (2 levels) in <top (required)>'
- The first tests are failing. We're seeing RED.
- Now we need to write code (in the starter code directory) to pass the tests and turn them GREEN.
- When a test passes, we're ready to stop and REFACTOR!
- Pay close attention to the rspec output in your terminal.
- Run your tests frequently and read the output carefully.
- BE VERY CAREFUL to use the correct function name.
- To limit your test output, consider filtering tests by method name with the
-e
flag:
rspec spec/argument_spec.rb -e get_fruit
When in doubt, test your code in the ruby REPL (irb
or pry
).
(This is similar to running the node repl by typing node
).
When you want to quit irb
or pry
, type and enter quit
.
Inside pry
, you can "hang a dot" and hit tab
twice to see available methods:
2.1.2 :001 > "string". # hang a dot and hit tab twice!
# Display all 148 possibilities? (y or n)
# "".match
# "".__id__ "".method
# "".__send__ "".methods
# "".ascii_only? "".next
# "".b "".next!
# "".between? "".nil?
# "".bytes "".object_id
# "".bytesize "".oct
# ...
Another handy trick is to load your Ruby file so that you can manually test your functions.
Make sure you're in the correct directory, then in pry
type:
2.1.2 :002 > load "./starter-code/arguments.rb"
2.1.2 :003 > say_hello
(Re-load
your file after you make changes.)
You can also run a ruby file from the command line by typing:
ruby starter-code/arguments.rb