Skip to content

Instantly share code, notes, and snippets.

@alireza-a
Last active March 12, 2018 01:00
Show Gist options
  • Save alireza-a/976feab389fc790d645bfc086b3afe39 to your computer and use it in GitHub Desktop.
Save alireza-a/976feab389fc790d645bfc086b3afe39 to your computer and use it in GitHub Desktop.
Ruby Koans checklist

generate checklist

$ grep -r 'def test_' ruby_koans/src > koans_def.txt

ruby script to generate the checklist

def koans_def(path: 'koans_def.txt')
    koans = Hash.new{|hash, key| hash[key] = Array.new}
    File.readlines(path).map do |line|
        about, koan = line.split(/def test/)
        about.gsub!('ruby_koans/src/', '').gsub!(/.rb:.+/, '')
        koan.gsub!('_', ' ')
        koans[about] << koan
    end
    koans.select {|about, koans| /about_/.match about }
end

def generate_checklist()
    File.open('ruby_koans_checklist.txt', 'w') do |f|
        koans_def.each do |about, koans|
            f.puts
            f.puts '## ' + about
            koans.each do |koan|
                f.puts '- [ ]' + koan
            end
        end
    end
end

generate_checklist()

ruby koans checklist

about_message_passing

  • methods can be called directly
  • methods can be invoked by sending the message
  • methods can be invoked more dynamically
  • send with underscores will also send messages
  • classes can be asked if they know how to respond
  • sending a message with arguments
  • sending undefined messages to a typical object results in errors
  • calling method missing causes the no method error
  • all messages are caught
  • catching messages makes respond to lie
  • foo method are caught
  • non foo messages are treated normally
  • explicitly implementing respond to lets objects tell the truth

about_java_interop

  • using a java library class
  • java class can be referenced using both ruby and java like syntax
  • include class includes class in module scope
  • also java class can be given ruby aliases
  • can directly call java methods on java objects
  • jruby provides snake case versions of java methods
  • jruby provides question mark versions of boolean methods
  • java string are not ruby strings
  • java strings can be compared to ruby strings maybe
  • however most methods returning strings return ruby strings
  • some ruby objects can be coerced to java
  • some ruby objects are not coerced to what you might expect
  • java collections are enumerable
  • java class are open from ruby

about_dice_project

  • can create a dice set
  • rolling the dice returns a set of integers between 1 and 6
  • dice values do not change unless explicitly rolled
  • dice values should change between rolls
  • you can roll different numbers of dice

about_strings

  • double quoted strings are strings
  • single quoted strings are also strings
  • use single quotes to create string with double quotes
  • use double quotes to create strings with single quotes
  • use backslash for those hard cases
  • use flexible quoting to handle really hard cases
  • flexible quotes can handle multiple lines
  • here documents can also handle multiple lines
  • plus will concatenate two strings
  • plus concatenation will leave the original strings unmodified
  • plus equals will concatenate to the end of a string
  • plus equals also will leave the original string unmodified
  • the shovel operator will also append content to a string
  • the shovel operator modifies the original string
  • double quoted string interpret escape characters
  • single quoted string do not interpret escape characters
  • single quotes sometimes interpret escape characters
  • double quoted strings interpolate variables
  • single quoted strings do not interpolate
  • any ruby expression may be interpolated
  • you can get a substring from a string
  • you can get a single character from a string
  • in older ruby single characters are represented by integers
  • in modern ruby single characters are represented by strings
  • strings can be split
  • strings can be split with different patterns
  • strings can be joined
  • strings are unique objects

about_classes

  • instances of classes can be created with new
  • instance variables can be set by assigning to them
  • instance variables cannot be accessed outside the class
  • you can politely ask for instance variable values
  • you can rip the value out using instance eval
  • you can create accessor methods to return instance variables
  • attr reader will automatically define an accessor
  • attr accessor will automatically define both read and write accessors
  • initialize provides initial values for instance variables
  • args to new must match initialize
  • different objects have different instance variables
  • inside a method self refers to the containing object
  • to s provides a string version of the object
  • to s is used in string interpolation
  • inspect provides a more complete string version
  • all objects support to s and inspect

about_modules

  • cant instantiate modules
  • normal methods are available in the object
  • module methods are also available in the object
  • module methods can affect instance variables in the object
  • classes can override module methods

about_array_assignment

  • non parallel assignment
  • parallel assignments
  • parallel assignments with extra values
  • parallel assignments with splat operator
  • parallel assignments with too few variables
  • parallel assignments with subarrays
  • parallel assignment with one variable
  • swapping with parallel assignment

about_open_classes

  • as defined dogs do bark
  • after reopening dogs can both wag and bark
  • even existing built in classes can be reopened

about_to_str

  • to s returns a string representation
  • normally objects cannot be used where strings are expected
  • to str also returns a string representation
  • to str allows objects to be treated as strings
  • user defined code can check for to str

about_symbols

  • symbols are symbols
  • symbols can be compared
  • identical symbols are a single internal object
  • method names become symbols
  • constants become symbols
  • symbols can be made from strings
  • symbols with spaces can be built
  • symbols with interpolation can be built
  • to s is called on interpolated symbols
  • symbols are not strings
  • symbols do not have string methods
  • symbols cannot be concatenated
  • symbols can be dynamically created

about_iteration

  • each is a method on arrays
  • iterating with each
  • each can use curly brace blocks too
  • break works with each style iterations
  • collect transforms elements of an array
  • select selects certain items from an array
  • find locates the first element matching a criteria
  • inject will blow your mind
  • all iteration methods work on any collection not just arrays

about_methods

  • calling global methods
  • calling global methods without parentheses
  • sometimes missing parentheses are ambiguous
  • calling global methods with wrong number of arguments
  • calling with default values
  • calling with variable arguments
  • method with explicit return
  • method without explicit return
  • calling methods in same class
  • calling methods in same class with explicit receiver
  • calling private methods without receiver
  • calling private methods with an explicit receiver
  • calling methods in other objects require explicit receiver
  • calling private methods in other objects

about_keyword_arguments

  • keyword arguments
  • keyword arguments with wrong number of arguments

about_nil

  • nil is an object
  • you dont get null pointer errors when calling methods on nil
  • nil has a few methods defined on it

about_objects

  • everything is an object
  • objects can be converted to strings
  • objects can be inspected
  • every object has an id
  • every object has different id
  • small integers have fixed ids
  • clone creates a different object

about_constants

  • nested constants may also be referenced with relative paths
  • top level constants are referenced by double colons
  • nested constants are referenced by their complete path
  • nested classes inherit constants from enclosing classes
  • subclasses inherit constants from parent classes
  • who wins with both nested and inherited constants
  • who wins with explicit scoping on class definition

about_exceptions

  • exceptions inherit from Exception
  • rescue clause
  • raising a particular error
  • ensure clause
  • asserting an error is raised #

about_scope

  • dog is not available in the current scope
  • you can reference nested classes using the scope operator
  • bare bones class names assume the current scope
  • nested string is not the same as the system string
  • use the prefix scope operator to force the global scope
  • constants are defined with an initial uppercase letter
  • class names are just constants
  • constants can be looked up explicitly
  • you can get a list of constants for any class or module

about_proxy_object_project

  • proxy method returns wrapped object
  • tv methods still perform their function
  • proxy records messages sent to tv
  • proxy handles invalid messages
  • proxy reports methods have been called
  • proxy counts method calls
  • proxy can record more than just tv objects
  • it turns on
  • it also turns off
  • edge case on off
  • can set the channel

about_triangle_project_2

  • illegal triangles throw exceptions

about_control_statements

  • if then else statements
  • if then statements
  • if statements return values
  • if statements with no else with false condition return value
  • condition operators
  • if statement modifiers
  • unless statement
  • unless statement evaluate true
  • unless statement modifier
  • while statement
  • break statement
  • break statement returns values
  • next statement
  • for statement
  • times statement

about_arrays

  • creating arrays
  • array literals
  • accessing array elements
  • slicing arrays
  • arrays and ranges
  • slicing with ranges
  • pushing and popping arrays
  • shifting arrays

about_hashes

  • creating hashes
  • hash literals
  • accessing hashes
  • accessing hashes with fetch
  • changing hashes
  • hash is unordered
  • hash keys
  • hash values
  • combining hashes
  • default value
  • default value is the same object
  • default value with block

about_scoring_project

  • score of an empty list is zero
  • score of a single roll of 5 is 50
  • score of a single roll of 1 is 100
  • score of multiple 1s and 5s is the sum of individual scores
  • score of single 2s 3s 4s and 6s are zero
  • score of a triple 1 is 1000
  • score of other triples is 100x
  • score of mixed is sum

about_true_and_false

  • true is treated as true
  • false is treated as false
  • nil is treated as false too
  • everything else is treated as true

about_triangle_project

  • equilateral triangles have equal sides
  • isosceles triangles have exactly two sides equal
  • scalene triangles have no equal sides

about_class_methods

  • objects are objects
  • classes are classes
  • classes are objects too
  • objects have methods
  • classes have methods
  • you can define methods on individual objects
  • other objects are not affected by these singleton methods
  • since classes are objects you can define singleton methods on them too
  • class methods are independent of instance methods
  • classes and instances do not share instance variables
  • you can define class methods inside the class
  • class statements return the value of their last expression
  • self while inside class is class object not instance
  • you can use self instead of an explicit reference to dog
  • heres still another way to write class methods
  • heres an easy way to call class methods from instance methods

about_inheritance

  • subclasses have the parent as an ancestor
  • all classes ultimately inherit from object
  • subclasses inherit behavior from parent class
  • subclasses add new behavior
  • subclasses can modify existing behavior
  • subclasses can invoke parent behavior via super
  • super does not work cross method

about_sandwich_code

  • counting lines
  • finding lines
  • counting lines2
  • finding lines2
  • open handles the file sandwich when given a block

about_regular_expressions

  • a pattern is a regular expression
  • a regexp can search a string for matching content
  • a failed match returns nil
  • question mark means optional
  • plus means one or more
  • asterisk means zero or more
  • the left most match wins
  • character classes give options for a character
  • slash d is a shortcut for a digit character class
  • character classes can include ranges
  • slash s is a shortcut for a whitespace character class
  • slash w is a shortcut for a word character class
  • period is a shortcut for any non newline character
  • a character class can be negated
  • shortcut character classes are negated with capitals
  • slash a anchors to the start of the string
  • slash z anchors to the end of the string
  • caret anchors to the start of lines
  • dollar sign anchors to the end of lines
  • slash b anchors to a word boundary
  • parentheses group contents
  • parentheses also capture matched content by number
  • variables can also be used to access captures
  • a vertical pipe means or
  • scan is like find all
  • sub is like find and replace
  • gsub is like find and replace all

about_blocks

  • methods can take blocks
  • blocks can be defined with do end too
  • blocks can take arguments
  • methods can call yield many times
  • methods can see if they have been called with a block
  • block can affect variables in the code where they are created
  • blocks can be assigned to variables and called explicitly
  • stand alone blocks can be passed to methods expecting blocks
  • methods can take an explicit block argument

about_asserts

  • assert truth
  • assert with message
  • assert equality
  • a better way of asserting equality
  • fill in values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment