Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .path_progress
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0,0,0,1,2,3,4,5,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,9,10,11,11,11,13,14,15,15,16,17,18,18,19,19,19,20,20,21,21,21,22,22,23,24,25,26,27,27,27,28,29,30,30,31,31,31,32,33,34,34,35,35,35,35,35,36,36,37,37,38,39,39,40,40,40,40,41,41,42,43,47,48,49,49,49,49,49,50,50,50,51,52,56,56,56,57,58,58,59,59,60,61,62,63,63,64,65,66,67,68,70,72,73,74,74,74,75,75,76,76,76,77,78,79,79,80,81,82,82,83,83,84,85,86,86,88,88,88,89,89,90,91,93,93,94,95,96,97,97,98,98,98,98,100,100,102,103,104,105,106,106,107,107,108,109,110,111,111,111,111,111,111,111,111,111,113,113,113,113,114,115,116,117,118,119,120,121,122,124,124,124,124,124,124,125,125,126,129,130,131,132,132,133,133,134,135,136,137,139,140,142,143,143,144,145,145,146,148,151,152,153,153,153,153,153,153,153,153,153,155,155,155,155,156,156,156,156,157,158,159,159,160,160,160,160,160,160,152,152,161,162,163,165,166,167,168,168,169,169,169,169,169,175,178,178,179,180,180,180,181,182,182,183,184,184,192,193,193,194,194,195,196,196,196,197,201,202,203,203,203,203,203,203,203,204,205,206,207,207,207,207,207,208,210,211,212,212,212,212,212,212,216,220,222,222,223,224,226,228,228,229,229,230,231,233,234,235,235,236,236,237,237,236,237,237,237,237,237,237,240,241,241,242,243,244,246,246,247,248,250,251,252,255,256,257,258,259,261,262,263,264,265,267,267,267,267,267,267,267,267,267,267,267,267,268,267,267,268,268,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,267,277,278,279,279,280,281,281,173,140,17,267,267,267,133,25,24,26,24,28,225,132,193,197,184,184,184,189,121,159,159,159,6,6,246,248,248,152,135
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable all
#!/usr/bin/env ruby
# -*- ruby -*-

Expand Down
49 changes: 26 additions & 23 deletions about_array_assignment.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
require File.expand_path(File.dirname(__FILE__) + '/neo')

# Class about array
class AboutArrayAssignment < Neo::Koan
def test_non_parallel_assignment
names = ["John", "Smith"]
assert_equal __, names
names = %w[John Smith]
assert_equal %w[John Smith], names
end

def test_parallel_assignments
first_name, last_name = ["John", "Smith"]
assert_equal __, first_name
assert_equal __, last_name
first_name = 'John'
last_name = 'Smith'
assert_equal 'John', first_name
assert_equal 'Smith', last_name
end

def test_parallel_assignments_with_extra_values
first_name, last_name = ["John", "Smith", "III"]
assert_equal __, first_name
assert_equal __, last_name
first_name, last_name = %w[John Smith III]
assert_equal 'John', first_name
assert_equal 'Smith', last_name
end

def test_parallel_assignments_with_splat_operator
first_name, *last_name = ["John", "Smith", "III"]
assert_equal __, first_name
assert_equal __, last_name
first_name, *last_name = %w[John Smith III]
assert_equal 'John', first_name
assert_equal %w[Smith III], last_name
end

def test_parallel_assignments_with_too_few_variables
first_name, last_name = ["Cher"]
assert_equal __, first_name
assert_equal __, last_name
first_name, last_name = %w[Cher]
assert_equal 'Cher', first_name
assert_equal nil, last_name
end

def test_parallel_assignments_with_subarrays
first_name, last_name = [["Willie", "Rae"], "Johnson"]
assert_equal __, first_name
assert_equal __, last_name
first_name = %w[Willie Rae]
last_name = 'Johnson'
assert_equal %w[Willie Rae], first_name
assert_equal 'Johnson', last_name
end

def test_parallel_assignment_with_one_variable
first_name, = ["John", "Smith"]
assert_equal __, first_name
first_name, = %w[John Smith]
assert_equal 'John', first_name
end

def test_swapping_with_parallel_assignment
first_name = "Roy"
last_name = "Rob"
first_name = 'Roy'
last_name = 'Rob'
first_name, last_name = last_name, first_name
assert_equal __, first_name
assert_equal __, last_name
assert_equal 'Rob', first_name
assert_equal 'Roy', last_name
end
end
80 changes: 40 additions & 40 deletions about_arrays.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,84 @@
require File.expand_path(File.dirname(__FILE__) + '/neo')

# Class about array
class AboutArrays < Neo::Koan
def test_creating_arrays
empty_array = Array.new
assert_equal __, empty_array.class
assert_equal __, empty_array.size
empty_array = []
assert_equal Array, empty_array.class
assert_equal 0, empty_array.size
end

def test_array_literals
array = Array.new
array = []
assert_equal [], array

array[0] = 1
assert_equal [1], array

array[1] = 2
assert_equal [1, __], array
assert_equal [1, 2], array

array << 333
assert_equal __, array
assert_equal [1, 2, 333], array
end

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

assert_equal __, array[0]
assert_equal __, array.first
assert_equal __, array[3]
assert_equal __, array.last
assert_equal __, array[-1]
assert_equal __, array[-3]
array = %i[peanut butter and jelly]

assert_equal :peanut, array[0]
assert_equal :peanut, array.first
assert_equal :jelly, array[3]
assert_equal :jelly, array.last
assert_equal :jelly, array[-1]
assert_equal :butter, array[-3]
end

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]
assert_equal __, array[4,0]
assert_equal __, array[4,100]
assert_equal __, array[5,0]
array = %i[peanut butter and jelly]

assert_equal [:peanut], array[0, 1]
assert_equal %i[peanut butter], array[0, 2]
assert_equal %i[and jelly], array[2, 2]
assert_equal %i[and jelly], array[2, 20]
assert_equal [], array[4, 0]
assert_equal [], array[4, 100]
assert_equal nil, array[5, 0]
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
assert_equal __, (1...5).to_a
assert_equal Range, (1..5).class
assert_not_equal [1, 2, 3, 4, 5], (1..5)
assert_equal [1, 2, 3, 4, 5], (1..5).to_a
assert_equal [1, 2, 3, 4], (1...5).to_a
end

def test_slicing_with_ranges
array = [:peanut, :butter, :and, :jelly]
array = %i[peanut butter and jelly]

assert_equal __, array[0..2]
assert_equal __, array[0...2]
assert_equal __, array[2..-1]
assert_equal %i[peanut butter and], array[0..2]
assert_equal %i[peanut butter], array[0...2]
assert_equal %i[and jelly], array[2..-1]
end

def test_pushing_and_popping_arrays
array = [1,2]
array = [1, 2]
array.push(:last)

assert_equal __, array
assert_equal [1, 2, :last], array

popped_value = array.pop
assert_equal __, popped_value
assert_equal __, array
assert_equal :last, popped_value
assert_equal [1, 2], array
end

def test_shifting_arrays
array = [1,2]
array = [1, 2]
array.unshift(:first)

assert_equal __, array
assert_equal [:first, 1, 2], array

shifted_value = array.shift
assert_equal __, shifted_value
assert_equal __, array
assert_equal :first, shifted_value
assert_equal [1, 2], array
end

end
17 changes: 8 additions & 9 deletions about_asserts.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
#!/usr/bin/env ruby
# -*- ruby -*-

# rubocop:disable Lint/UnneededCopDisableDirective, Lint/ScriptPermission
require File.expand_path(File.dirname(__FILE__) + '/neo')

# Class about asserts
class AboutAsserts < Neo::Koan

# We shall contemplate truth by testing reality, via asserts.
def test_assert_truth
assert false # This should be true
assert true # This should be true
end

# Enlightenment may be more easily achieved with appropriate
# messages.
def test_assert_with_message
assert false, "This should be true -- Please fix this"
assert true, 'This should be true -- Please fix this'
end

# To understand reality, we must compare our expectations against
# reality.
def test_assert_equality
expected_value = __
expected_value = 2
actual_value = 1 + 1

assert expected_value == actual_value
end

# Some ways of asserting equality are better than others.
def test_a_better_way_of_asserting_equality
expected_value = __
expected_value = 2
actual_value = 1 + 1

assert_equal expected_value, actual_value
end

# Sometimes we will ask you to fill in the values
def test_fill_in_values
assert_equal __, 1 + 1
assert_equal 2, 1 + 1
end
end
# rubocop:enable Lint/UnneededCopDisableDirective, Lint/ScriptPermission
40 changes: 20 additions & 20 deletions about_blocks.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + '/neo')

# Class about block
class AboutBlocks < Neo::Koan
def method_with_block
result = yield
Expand All @@ -8,23 +9,23 @@ def method_with_block

def test_methods_can_take_blocks
yielded_result = method_with_block { 1 + 2 }
assert_equal __, yielded_result
assert_equal 3, yielded_result
end

def test_blocks_can_be_defined_with_do_end_too
yielded_result = method_with_block do 1 + 2 end
assert_equal __, yielded_result
yielded_result = method_with_block { 1 + 2 }
assert_equal 3, yielded_result
end

# ------------------------------------------------------------------

def method_with_block_arguments
yield("Jim")
yield('Jim')
end

def test_blocks_can_take_arguments
method_with_block_arguments do |argument|
assert_equal __, argument
assert_equal 'Jim', argument
end
end

Expand All @@ -40,7 +41,7 @@ def many_yields
def test_methods_can_call_yield_many_times
result = []
many_yields { |item| result << item }
assert_equal __, result
assert_equal %i[peanut butter and jelly], result
end

# ------------------------------------------------------------------
Expand All @@ -54,43 +55,42 @@ def yield_tester
end

def test_methods_can_see_if_they_have_been_called_with_a_block
assert_equal __, yield_tester { :with_block }
assert_equal __, yield_tester
assert_equal :with_block, (yield_tester { :with_block })
assert_equal :no_block, yield_tester
end

# ------------------------------------------------------------------

def test_block_can_affect_variables_in_the_code_where_they_are_created
value = :initial_value
method_with_block { value = :modified_in_a_block }
assert_equal __, value
assert_equal :modified_in_a_block, value
end

def test_blocks_can_be_assigned_to_variables_and_called_explicitly
add_one = lambda { |n| n + 1 }
assert_equal __, add_one.call(10)
add_one = ->(elem) { elem + 1 }
assert_equal 11, add_one.call(10)

# Alternative calling syntax
assert_equal __, add_one[10]
assert_equal 11, add_one[10]
end

def test_stand_alone_blocks_can_be_passed_to_methods_expecting_blocks
make_upper = lambda { |n| n.upcase }
make_upper = ->(elem) { elem.upcase }
result = method_with_block_arguments(&make_upper)
assert_equal __, result
assert_equal 'JIM', result
end

# ------------------------------------------------------------------

def method_with_explicit_block(&block)
block.call(10)
def method_with_explicit_block
yield(10)
end

def test_methods_can_take_an_explicit_block_argument
assert_equal __, method_with_explicit_block { |n| n * 2 }
assert_equal 20, (method_with_explicit_block { |elem| elem * 2 })

add_one = lambda { |n| n + 1 }
assert_equal __, method_with_explicit_block(&add_one)
add_one = ->(elem) { elem + 1 }
assert_equal 11, method_with_explicit_block(&add_one)
end

end
Loading