Skip to content

Instantly share code, notes, and snippets.

@Khvalin
Created October 17, 2011 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Khvalin/5210642f5fe64ca8303b to your computer and use it in GitHub Desktop.
Save Khvalin/5210642f5fe64ca8303b to your computer and use it in GitHub Desktop.
Methods taking multiple blocks -- http://codebrawl.com

Notes

This is a simple method taking multiple blocks demonstration.

The script takes one parameter: it could be an age or a birth date(DD-MM-YYYY). Then it tries to find out what age range does the person belongs to.

When the input string is submitted, the program runs a series of tests ("modifiers") to find out what type of data has been entered: a date or a number.

Each modifier validates input and throws and exception when the input is invalid. The recursive "Validate" method calls the cascade of modifiers according to the validator's tree. For demonstration purpose, every test result is shown.

##Usage

$ ruby ./blocks.rb 10.10.1980
isNotNull: passed
 isNumber: failed
 isDate: passed
  isAge: passed
   isChildAge: failed
   isAdultAge: passed
   isSeniorAge: failed


$ ruby ./blocks.rb 8
isNotNull: passed
 isNumber: passed
  isAge: passed
   isChildAge: passed
   isAdultAge: failed
   isSeniorAge: failed
 isDate: failed
require 'Date'
def Validate(input, validators, validatorTree, i = 0)
validatorTree.each {|validatorId, subValidators|
validator = validators[validatorId]
validationResult = nil
testPassed = true
validationResult = validator.call(input) rescue testPassed = false
puts " " * i + validatorId.to_s + ": " + (testPassed ? "passed":"failed")
Validate(validationResult, validators, subValidators, i + 1) if (testPassed && !subValidators.nil?)
}
end
checkNotNull = Proc.new { |input| raise(ArgumentError) if input.to_s.strip.empty?; input }
checkNumberProc = Proc.new { |input| Integer(input) }
checkAgeProc = Proc.new { |input| raise ArgumentError if (input > 120 || input < 0); input}
checkForChildAgeProc = Proc.new { |input| raise ArgumentError if (input > 18); input}
checkForAdultAgeProc = Proc.new { |input| raise ArgumentError if (input <= 18 || input >= 65); input}
checkForSeniorAgeProc = Proc.new { |input| raise ArgumentError if (input < 65); input}
checkDateProc = Proc.new { |input| ((Date.today - Date.parse(input)) / 365.37).to_i }
allTests = {:isNotNull => checkNotNull, :isNumber => checkNumberProc, :isDate => checkDateProc,
:isAge => checkAgeProc, :isChildAge => checkForChildAgeProc, :isAdultAge=>checkForAdultAgeProc, :isSeniorAge => checkForSeniorAgeProc}
ageCheckHash = {:isAge => {:isChildAge=>nil, :isAdultAge => nil, :isSeniorAge => nil} }
testTree = {:isNotNull => {:isNumber => ageCheckHash , :isDate => ageCheckHash } }
inputStr = ARGV[0]
if (inputStr.nil?)
puts "enter a date in DD-MM-YYYY format for birthdate or a number for age:"
inputStr = gets
end
Validate inputStr, allTests, testTree
@rogerbraun
Copy link

Very specific and somewhat hard to read.

@JEG2
Copy link

JEG2 commented Oct 17, 2011

I was hoping to see more of a generic solution to the problem. This feels more like how I would manually code it up.

@jeremyevans
Copy link

This passes a hash of proc objects to the method, which goes against the instructions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment