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
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") |
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
#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 |
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
module Scrollable | |
def scroll offset | |
# scrollling code | |
end | |
end | |
class Pane | |
attr_accessor :content | |
def initialize content | |
@content = content |
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
class Test | |
def public_method | |
t = Test.new | |
t.private_method # illegal (throws NoMethodError) | |
end | |
private | |
def private_method |
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
class Test | |
def self.main | |
Test.new.method #illegal (throws NoMethodError) | |
end | |
private | |
def method | |
puts "private method called" |
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
public class Test{ | |
private void method(){ System.out.println("method called"); } | |
public static void main(String[] args){ | |
Test test = new Test(); | |
test.method(); | |
} | |
} |
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
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) |
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
require 'rubygems' | |
require 'active_support' | |
class Time | |
@@WEEKDAYS = (1..5) | |
def next_business_day days_count = 1 | |
day = self | |
days_count.times do | |
begin |
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
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) |
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
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 |