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 Module < Object | |
def module_eval | |
# Only the language has the magic to make this work... | |
end | |
# Imagine there are a bunch of other instance methods here. | |
# Module.new generates objects that respond to these methods. | |
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
UselessAble.module_eval do | |
# This is written in the module's instance methods collection | |
def guess_what? | |
puts "I'm useless!" | |
end | |
end | |
def UselessAble.who? | |
puts 'I am an abstract behavior, but I do exist somehow!' | |
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
asshole_1 = AssHole.new | |
asshole_1.guess_what? #=> outputs "I'm useless!" | |
asshole_1.soul #=> outputs "I have no soul!" | |
asshole_2 = AssHole.new | |
asshole_2.guess_what? #=> outputs "I'm useless!" | |
asshole_2.soul #=> outputs "I have no soul!" |
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
module UselessAble | |
def guess_what? | |
puts "I'm useless!" | |
end | |
end | |
# Instances of AssHole now respond to "guess_what?" and "soul" | |
class AssHole | |
include UselessAble |
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
# Instances of Project respond to every method of Imageable! | |
class Project | |
include Imageable | |
end | |
# Provides the same collection of methods as Imageable | |
module SuperImageable | |
include Imageable | |
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
Imageable = Module.new | |
def Imageable.included(model_class) | |
model_class.has_many :images | |
end | |
Imageable.module_eval do | |
def main_image | |
images.find { |image| image.kind == 'main_image' } | |
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
class Project < ApplicationRecord | |
include Imageable | |
end | |
project = Project.new | |
project.main_image #=> <Image kind="main_image" imageable_type="Project"> |
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
module Imageable | |
# When including this module, this hook is called | |
# by Ruby with the included module/class | |
def self.included(model_class) | |
model_class.has_many :images | |
end | |
def main_image | |
images.find { |image| image.kind == 'main_image' } | |
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
import { expect } from 'chai'; | |
import renderOneTweet from 'app/twitter/renderOneTweet'; | |
import createFixture from '../../support/createFixture'; | |
describe('renderOneTweet', () => { | |
let fixture; | |
beforeEach(() => { fixture = createFixture({ html: '<div id="fixture"></div>' }); }); | |
afterEach(() => { fixture.remove(); }); |