Skip to content

Instantly share code, notes, and snippets.

View ngpestelos's full-sized avatar

Nestor G Pestelos Jr ngpestelos

View GitHub Profile
@ngpestelos
ngpestelos / assign_block_2.rb
Created September 19, 2013 09:56
assign a block, redux
x = lambda { "hello again" }
puts x.call
@ngpestelos
ngpestelos / assign_block.rb
Created September 19, 2013 09:42
assign a block
x = Proc.new { "hello" }
puts x.call
@ngpestelos
ngpestelos / hello_block.rb
Created September 18, 2013 10:33
hello block
def hello
puts "Hello, #{yield}!"
end
hello { "world" }
@ngpestelos
ngpestelos / deprecate.rb
Created September 18, 2013 10:09
deprecate methods
# see Metaprogramming Ruby, p. 104
class Book
def title
"title"
end
def subtitle
"subtitle"
@ngpestelos
ngpestelos / create_class_macros.rb
Created September 18, 2013 08:36
creating class macros
# See http://techie.lucaspr.im/creating-class-macros/
class MyClass
def self.create_hello_method_for(name)
define_method(:hello) do
puts "Hello #{name}"
end
end
create_hello_method_for :john
@ngpestelos
ngpestelos / attr_accessor.rb
Created September 18, 2013 08:21
module attr_accessor
class MyClass
attr_accessor :my_attribute
end
@ngpestelos
ngpestelos / boilerplate.rb
Last active December 23, 2015 08:09
class boilerplate code
# see Metaprogramming Ruby, p. 103
class MyClass
def my_attribute=(value)
@my_attribute = value
end
def my_attribute
@my_attribute
end
@ngpestelos
ngpestelos / time3.rb
Created September 14, 2013 04:00
time 3
# see Metaprogramming Ruby, p. 98
class FakeTime
def self.now; 'Mon Apr 06 12:15:50'; end
end
require 'test/unit'
class TestLoan < Test::Unit::TestCase
def test_conversion_to_string
@ngpestelos
ngpestelos / time2.rb
Last active December 23, 2015 01:09
fake time
# see Metaprogramming Ruby, p. 98
class Loan
def initialize(book)
@book = book
@time = Loan.time.now
end
def self.time
@time_class || Time
@ngpestelos
ngpestelos / time.rb
Created September 14, 2013 03:44
time
# see Metaprogramming Ruby, p. 96
class Loan
def initialize(book)
@book = book
@time = Time.now
end
def to_s
"#{@book.upcase} loaned on #{@time}"