Created
September 20, 2012 22:25
-
-
Save koriroys/3758728 to your computer and use it in GitHub Desktop.
It's a Tarp!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require File.join(File.dirname(__FILE__), 'test1') | |
puts "1: #{ Test::One.sort([4, 1, 5.5, 600_000]) }" | |
puts "2: #{ Test::Two.what_am_i }" | |
Test::Three.extend_string | |
puts "3: #{ "Hello".to_s }" | |
puts "4: #{ Test::Four.extend_vs_include }" | |
puts "5: #{ Test::Five.reverse('reverse me') }" | |
puts "6: #{ Test::Six.divide_hash }" | |
puts "7: #{ Test::Seven.common_elements([1,2,3], 1, 2) }" | |
puts "8: #{ Test::Eight.optional_block }" | |
puts "8: #{ Test::Eight.optional_block { puts "hello" } }" | |
class Person | |
def name | |
"Bob" | |
end | |
end | |
puts "9: #{ Test::Nine.responds_to_name([1,2,3], "hello", Person.new) }" | |
puts "10: #{ Test::Ten.sarcastic_wonka("string", [5,2,1], { q: "uote", m: "e" }) }" | |
require 'socket' | |
puts "11: #{ ["hello", [1, 2], {}, Socket.new(1, 2), Person.new].map { |e| Test::Eleven.ceiling_cat(e) } }" | |
puts "12: #{ Test::Twelve.hola_papi('monday') }" | |
puts "12: #{ Test::Twelve.hola_mami('saturday') }" # ahh, too clever. ah! | |
class Dummy | |
include Test::Thirteen | |
end | |
puts "13: #{ Dummy.new.awesometown }" | |
puts "14: #{ Test::Fourteen.new.run_tests }" | |
puts "15: #{ Test::Fifteen.cookie({ :A => 1, :B => 2, :C => 3 }) }" | |
puts "16: #{ Test::Sixteen.good({ :A => 1, :B => 2, :C => 3 }) }" | |
puts "17: #{ Test::Seventeen.state_of({ :A => 1, :B => 2, :C => 3 }) }" | |
puts "18: #{ Test::Eighteen.who({ :A => 1, :B => 2, :hello => 3 }) }" | |
puts "18: #{ Test::Eighteen.who({ :A => 1, :B => 2, :C => 3 }) }" | |
puts "19: #{ Test::Nineteen.new.run_tests }" | |
puts "20: #{ Test::Twenty.keywords }" | |
puts "21: #{ Test::TwentyOne::D.new.hello }" | |
puts "21: #{ }" #Test::TwentyOne::A.hello # this will throw NoMethodError | |
puts "21: #{ Test::TwentyOne::B.new.hello }" | |
puts "21: #{ Test::TwentyOne::C.new.hello }" | |
puts "21: #{ Test::TwentyOne::C.new.world }" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
module TestHelper | |
def run_tests | |
@tests.map do |test| | |
begin | |
eval test | |
rescue | |
"This gives an error. Fix your quiz!" | |
end | |
end | |
end | |
end | |
module Test | |
# 1. Sort an Array of Numbers | |
class One | |
def self.sort(input) | |
input.sort | |
end | |
end | |
# 2. Explain what a Class is? | |
class Two | |
def self.what_am_i | |
"A class is a way to represent an object and package with it related behavior" | |
end | |
end | |
# 3. Add new functionality to the String#to_s method? (print the string contents prepended by the length) | |
class Three | |
def self.extend_string | |
String.send(:define_method, :to_s) { "#{self.length}#{self}" } | |
end | |
end | |
# 4. Explain the difference between Extend and Include keywords | |
class Four | |
def self.extend_vs_include | |
"I actually have to Google this each time cause I forget" | |
end | |
end | |
# 5. Reverse a string (example: "cat" => "tac") | |
class Five | |
def self.reverse(input) | |
input.reverse | |
end | |
end | |
# 6. Divide a Hash into 2 Hashes, 1 with even length keys and the other with the odd length keys | |
#{odd: 3, even: 4, odder: 5, odderstill: 10 }.partition {|k| k} # => | |
class Six | |
def self.divide_hash | |
"this is hard" | |
end | |
end | |
# 7. Write a method that takes an array and any number of additional arguments. print out all elements that are present (exist) in both the array and the additional arguments (example: [1, 2, 3], 1, 2 => 1, 2) | |
class Seven | |
def self.common_elements(elements, *args) | |
elements & args | |
end | |
end | |
# 8. Write a method that takes an optional block, if the optional block is given then execute it 12 times. | |
class Eight | |
def self.optional_block(&block) | |
12.times { block.call } if block | |
end | |
end | |
# 9. Write a method that takes an array of objects. If the object has a property or method "name" then print the name, otherwise print the string representation of the object followed by "NO NAME". | |
class Nine | |
def self.responds_to_name(*args) | |
args.map do |arg| | |
arg.respond_to?(:name) ? arg.name : "#{arg.inspect} NO NAME" | |
end | |
end | |
end | |
# 10. Write a method that takes an array of objects and returns an array of the passed objects lengths | |
class Ten | |
def self.sarcastic_wonka(*objects) | |
objects.map(&:length) | |
end | |
end | |
# 11. Write a method that takes an object as a parameter: if the object is a String then print "String be found, oh happy day", if the object is an Array then print "Arrays are almost the best data type ever", if the object is a Hash then print "Hashes are my Friend", if the object is a Socket then print "What? WHat? WHAT?", otherwise print "apple" | |
class Eleven | |
require 'socket' | |
def self.ceiling_cat(cat_food) | |
case cat_food | |
when String then "String be found, oh happy day" | |
when Array then "Arrays are almost the best data type ever" | |
when Hash then "Hashes are my Friend" | |
when Socket then "What? WHat? WHAT?" | |
else "apple" | |
end | |
end | |
end | |
# 12. Write a method that takes a day of the week as a string and translates it to Spanish and prints the Spanish. | |
class Twelve | |
def self.hola_papi(day) | |
case day | |
when 'monday' then 'lunes' | |
when 'tuesday' then 'martes' | |
when 'wednesday' then 'miercoles' | |
when 'thursday' then 'jueves' | |
when 'friday' then 'viernes' | |
when 'saturday' then 'sabado' | |
when 'sunday' then 'domingo' | |
end | |
end | |
# might as well have some fun with it! :) | |
def self.hola_mami(day) | |
Hash[*%w(monday lunes tuesday martes wednesday miercoles thursday jueves friday viernes saturday sabado sunday domingo)][day] | |
end | |
end | |
# 13. Write a module that can be included in any class and prints out the name of the class it has been included in when the method "awesometown" is called on an instance of the object. | |
module Thirteen | |
def awesometown | |
self.class | |
end | |
end | |
# 14. Write the results of the following to the right of the expression (or nil in the case where nothing is printed) | |
# puts "hello" if 1 == 1 | |
# puts "world" unless 1 == 1 | |
# puts "hello" if n = 1 | |
# puts "world" if n = nil | |
# puts n if n = 1 | |
# puts "hello" if "1" == 1 | |
# puts "world" if "1" === String | |
# puts "hello" if n = 1 && n == 1 | |
# puts "world" if true || false | |
# puts "hello" if 1 == 2 || 2 == 1 || 1 == 1 | |
# puts "world" if "hello" | |
# puts "hello" if "hello".nil? | |
# puts "world" if nil.nil? | |
# puts "hello" if 1 == 1 && 1 == 1 | |
class Fourteen | |
include TestHelper | |
def initialize | |
@tests = ['"hello" if 1 == 1', | |
'"world" unless 1 == 1', | |
'"hello" if n = 1', | |
'"world" if n = nil', | |
'n if n = 1', | |
'"hello" if "1" == 1', | |
'"world" if "1" === String', | |
'"hello" if n = 1 && n == 1', | |
'"world" if true || false', | |
'"hello" if 1 == 2 || 2 == 1 || 1 == 1', | |
'"world" if "hello"', | |
'"hello" if "hello".nil?', | |
'"world" if nil.nil?', | |
'"hello" if 1 == 1 && 1 == 1' | |
] | |
end | |
end | |
# 15. Given the Hash { :A => 1, :B => 2, :C => 3 }, print the key/value pairs as A::1, B::2, C::3 | |
class Fifteen | |
def self.cookie(monster) | |
monster.map { |k, v| "#{k}::#{v}" }.join(", ") | |
end | |
end | |
# 16. Given the Hash { :A => 1, :B => 2, :C => 3 }, return an Array of the values | |
class Sixteen | |
def self.good(catholic) | |
# catholic.map { |_, value| value } | |
catholic.values | |
end | |
end | |
# 17. Given the Hash { :A => 1, :B => 2, :C => 3 }, return an Array of the keys | |
class Seventeen | |
def self.state_of(florida) | |
florida.keys | |
end | |
end | |
# 18. Write a method that takes a Hash as an argument and return true or false depending on whether the key :hello is present in the Hash | |
class Eighteen | |
def self.who(hash) | |
hash.keys.any?{ |key| key == :hello } | |
end | |
end | |
# 19. Write the results of the following expressions to the right of the expression: | |
# (3/1).to_i | |
# (10/2).to_i | |
# (10/0).to_f | |
# (10/3).to_i | |
# (75/5).to_f | |
# (75/5.0).class | |
# (5 * "4") | |
class Nineteen | |
include TestHelper | |
def initialize | |
@tests = [ | |
'(3/1).to_i', | |
'(10/2).to_i', | |
'(10/0).to_f', | |
'(10/3).to_i', | |
'(75/5).to_f', | |
'(75/5.0).class', | |
'(5 * "4")' | |
] | |
end | |
end | |
# 20. Write 4 Ruby keywords (reserved words) | |
class Twenty | |
def self.keywords | |
"Really? REally? REALLY? class def self end" | |
end | |
end | |
# 21. What is printed? | |
# class A | |
# def hello | |
# puts "hello guys!" | |
# end | |
# end | |
# class B < A | |
# def world | |
# puts "Worldy world" | |
# end | |
# end | |
# class C < B | |
# def hello | |
# puts world | |
# end | |
# end | |
# class D < A | |
# def hello | |
# super | |
# puts "Worldy world" | |
# end | |
# end | |
# D.new.hello | |
# A.hello | |
# B.new.hello | |
# C.new.hello | |
# C.new.world | |
module TwentyOne | |
class A | |
def hello | |
"hello guys!" | |
end | |
end | |
class B < A | |
def world | |
"Worldy world" | |
end | |
end | |
class C < B | |
def hello | |
world | |
end | |
end | |
class D < A | |
def hello | |
super | |
"Worldy world" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment