Skip to content

Latest commit

 

History

History
188 lines (139 loc) · 7.08 KB

File metadata and controls

188 lines (139 loc) · 7.08 KB

This course was developed using MRI Ruby version 1.9.2 but most of what is taught here applies to earlier versions as well. Check your ruby version by invoking ruby -v on the command line. A good way to install ruby is to use the Ruby Version Manager (RVM). Please consult the official download and installation instructions to find the best installation option for your operating system.

You can run ruby commands interactively with the irb (Interactive Ruby) command or execute ruby scripts with the ruby command. Both of these commands can be invoked on the command line and they are also available inside some editors and IDEs. Both commands accept a --help option that gives you a good overview of how they work.

Examples of editors that you can use are vim, Emacs, TextMate (Mac only), Crimson (Windows only), jEdit, and RedCar. If you prefer an IDE then take a look at RubyMine, Aptana, or the RDT plugin for Eclipse (see What Ruby IDE do you prefer? on Stack Overflow for more options).

As you are working through the exercises and the code examples, make sure to consult the Ruby Core Reference as well as the Ruby Standard Library Reference as needed.

Assuming you are standing in the exercises directory (the Ruby Koans), here is an example of how to run an exercise from the command line:

ruby -w koans/about_asserts.rb

When you run the test case you get a line number in the file where a test is failing. Your job is to open the test file, edit it to make the failing test pass, and then re-run the test. Usually you’ll find “__” placeholders in the file where you should fill in the correct value. Continue editing and re-running the test case until all tests pass. Then move on to the next file in the list. Check out the Ruby Koans homepage for more information about the exercises.

  • lib/02_structure_and_execution

  • lib/03_datatypes

  • lib/04_control_structures

  • koans/about_asserts.rb

  • koans/about_strings.rb

  • koans/about_arrays.rb

  • koans/about_iteration.rb

  • koans/about_hashes.rb

  • koans/about_control_statements.rb

  • koans/about_exceptions.rb

  • koans/about_symbols.rb

  • koans/about_regular_expressions.rb

  • lib/05_variables_and_constants

  • lib/06_boolean_expressions_and_assignments

  • koans/about_true_and_false.rb

  • koans/about_constants.rb

  • koans/about_nil.rb

  • koans/about_array_assignment.rb

  • lib/07_classes_and_objects

  • lib/08_modules

  • lib/09_methods_and_operators

  • koans/about_methods.rb

  • koans/about_classes.rb

  • koans/about_class_methods.rb

  • koans/about_modules.rb

  • koans/about_inheritance.rb

  • lib/10_blocks_and_proc_objects

  • lib/11_reflection_and_meta_programming

  • lib/12_standard_library

  • lib/13_rubygems_and_bundler

  • koans/about_blocks.rb

  • koans/about_sandwich_code.rb

  • koans/about_objects.rb

  • koans/about_open_classes.rb

  • koans/about_message_passing.rb

  • koans/about_proxy_object_project.rb

  • koans/about_triangle_project.rb

  • koans/about_triangle_project_2.rb

  • koans/about_scoring_project.rb

  • koans/about_dice_project.rb

  • koans/about_to_str.rb

  • koans/about_scope.rb

  • koans/about_extra_credit.rb

Below you will find snippets of code from the Ruby Koans exercises with helpful notes where students tend to get stuck. The notes are marked up with the keyword HELP!.

###########################################################
# about_arrays.rb
###########################################################

def test_slicing_arrays
  array = [:peanut, :butter, :and, :jelly]

  assert_equal __, array[0,1]
  assert_equal __, array[0,2]
  assert_equal __, array[2,2]
  assert_equal __, array[2,20]
  # HELP! see http://stackoverflow.com/questions/3568222/array-slicing-in-ruby-looking-for-explanation-for-illogical-behaviour-taken-fro
  assert_equal __, array[4,0] # HELP! this will be an empty array as 4 is a valid index for a sub array (it's right after the last object)
  assert_equal __, array[4,100]
  assert_equal __, array[5,0] # HELP! this will be nil as 5 is not a valid index in the array
end

def test_arrays_and_ranges
  assert_equal __, (1..5).class
  assert_not_equal [1,2,3,4,5], (1..5)
  assert_equal __, (1..5).to_a # HELP! two dots means the range end point is included (inclusive range)
  assert_equal __, (1...5).to_a # HELP! three dots means that the range end point is not included (exclusive range)
end

###########################################################
# about_iteration.rb
###########################################################

# HELP! you may need to read up on how inject works here, see:
#
# http://www.ruby-doc.org/core/classes/Enumerable.html
#
# Why do we need Ruby inject? Ruby inject offers a concise alternative to a traditional loop working on a local variable, see:
#
# https://gist.github.com/d1ee408e5ccabf056262
#
def test_inject_will_blow_your_mind
  result = [2, 3, 4].inject(0) { |sum, item| sum + item }
  assert_equal __, result

  result2 = [2, 3, 4].inject(1) { |product, item| product * item }
  assert_equal __, result2

  # Extra Credit:
  # Describe in your own words what inject does.
end

# HELP! here you actually need to leave the file and go out and create the
# example_file.txt in your current directory and fill it with a few lines
def test_all_iteration_methods_work_on_any_collection_not_just_arrays
  # Ranges act like a collection
  result = (1..3).map { |item| item + 10 }
  assert_equal __, result

  # Files act like a collection of lines
  File.open("example_file.txt") do |file|
    upcase_lines = file.map { |line| line.strip.upcase }
    assert_equal __, upcase_lines
  end

  # NOTE: You can create your own collections that work with each,
  # map, select, etc.
end

###########################################################
# about_hashes.rb
###########################################################

def test_default_value
  hash1 = Hash.new
  hash1[:one] = 1

  assert_equal __, hash1[:one]
  assert_equal __, hash1[:two]

  # HELP! Here we are creating a hash here with a default value of "dos". This means that if we try to
  # read a key from the hash that doesn't exist, then we get "dos" back (instead of nil which is the default).
  # For more info on Ruby Hash, see:
  #
  # http://www.ruby-doc.org/core/classes/Hash.html
  #
  hash2 = Hash.new("dos")
  hash2[:one] = 1

  assert_equal __, hash2[:one]
  assert_equal __, hash2[:two]
end