Skip to content

Instantly share code, notes, and snippets.

@cawel
cawel / gist:678a84f5387bb25353c6
Created November 25, 2015 23:42
blog-quiz-easy-4
irb(main):001:0> 3.to_i
=> 3
irb(main):002:0> Integer(4)
=> 4
irb(main):003:0> "not_an_int".to_i
=> 0
irb(main):004:0> Integer("not_an_int")
@cawel
cawel / gist:5f5d8bc9ffe609551625
Created November 25, 2015 23:16
blog-quiz-intermediate-1b
#instance-specific behavior
puts "kind of Scrollable? #{pane.kind_of?(Scrollable)}"
pane.extend(Scrollable)
pane.scroll 3
puts pane.position
puts "kind of Scrollable? #{pane.kind_of?(Scrollable)}"
#adding behavior to the class
class Pane
@cawel
cawel / gist:f325b16cb17408dad7ee
Created November 25, 2015 23:15
blog-quiz-intermediate-1a
module Scrollable
def scroll offset
# scrollling code
end
end
class Pane
attr_accessor :content
def initialize content
@content = content
@cawel
cawel / gist:f0f24ebcc27789cf9669
Created November 25, 2015 23:08
glob-quiz-intermediate-2c
class Test
def public_method
t = Test.new
t.private_method # illegal (throws NoMethodError)
end
private
def private_method
@cawel
cawel / gist:5464963d498f28ef18d8
Last active November 25, 2015 23:04
blog-quiz-intermediate-2b
class Test
def self.main
Test.new.method #illegal (throws NoMethodError)
end
private
def method
puts "private method called"
@cawel
cawel / gist:9a57ac2d1ab28848f189
Created November 25, 2015 22:56
blog-quiz-intermediate-2a
public class Test{
private void method(){ System.out.println("method called"); }
public static void main(String[] args){
Test test = new Test();
test.method();
}
}
@cawel
cawel / gist:dab26d792ab2d4a5aa9e
Last active November 25, 2015 22:53
blog-quiz-intermediate-3
hash = Hash.new
# hash.has_key? 'no_such_key' => false
# default value is implicitly set to nil
# hash['no_such_key'] => nil
# hash.has_key? 'no_such_key' => false
hash = Hash.new(0)
# hash.has_key? 'no_such_key' => false
# default value is explicitly set (to 0 here)
@cawel
cawel / gist:0b22758afae201f9f5e0
Created November 25, 2015 22:34
blog-quiz-easy-5b
require 'rubygems'
require 'active_support'
class Time
@@WEEKDAYS = (1..5)
def next_business_day days_count = 1
day = self
days_count.times do
begin
@cawel
cawel / gist:cfb0ba54b73876fc93b6
Last active November 25, 2015 22:32
blog-quiz-easy-5a
require 'test/unit'
require 'rubygems'
require 'active_support'
require 'business_days' #the monkey patch code to be tested
class TestBusinessDays < Test::Unit::TestCase
def setup
@monday = Time.mktime(2008, 7, 14, 0, 0, 0)
@wednesday = Time.mktime(2008, 7, 16, 0, 0, 0)
@cawel
cawel / gist:d333f472a10b62b93899
Last active November 25, 2015 22:19
blog-quiz-intermediate-4b
class Decorator
  def initialize(subject)
    subject.public_methods(false).each do |meth|
      (class << self; self; end).class_eval do
        define_method meth do |*args|
          subject.send meth, *args
        end
      end
    end