This file contains hidden or 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
x = lambda { "hello again" } | |
puts x.call |
This file contains hidden or 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
x = Proc.new { "hello" } | |
puts x.call |
This file contains hidden or 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
def hello | |
puts "Hello, #{yield}!" | |
end | |
hello { "world" } |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 104 | |
class Book | |
def title | |
"title" | |
end | |
def subtitle | |
"subtitle" |
This file contains hidden or 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
# 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 |
This file contains hidden or 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 MyClass | |
attr_accessor :my_attribute | |
end |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 103 | |
class MyClass | |
def my_attribute=(value) | |
@my_attribute = value | |
end | |
def my_attribute | |
@my_attribute | |
end |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 98 | |
class Loan | |
def initialize(book) | |
@book = book | |
@time = Loan.time.now | |
end | |
def self.time | |
@time_class || Time |
This file contains hidden or 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
# see Metaprogramming Ruby, p. 96 | |
class Loan | |
def initialize(book) | |
@book = book | |
@time = Time.now | |
end | |
def to_s | |
"#{@book.upcase} loaned on #{@time}" |