/ruby_method_drills

31 ruby methods with tests

Primary LanguageRuby

Ruby Method Drills

Overview

Write code in each starter code file to pass the tests in the spec folder!

Objectives

This will be way more practice with Ruby methods and test driven development - and a great opportunity to explore some new tactics!

Setup

Fork this repo on GitHub.

git clone YOUR_FORK_CLONE_URL.git
cd ruby_method_drills

The files you'll work on are:

  1. arguments.rb -- (test file is argument_spec.rb)

  2. strings.rb -- (test file is string_spec.rb)

  3. iterators.rb -- (test file is iterator_spec.rb)

  4. numbers.rb -- (test file is number_spec.rb)

  5. hashes.rb -- (test file is hash_spec.rb)

Testing

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.

Test Driven Development -- "Red, Green, Refactor"

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)>'

Congrats!

  • 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!

Rspec Tips

  • 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

Ruby Tips

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