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
- if @post.is_recent? | |
= image_tag(@post.image) |
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 Post < ActiveRecord::Model | |
def recent? | |
published_at > 2.days.ago | |
end | |
end | |
class PostsController < ApplicationController | |
def show | |
@post = Post.find(params[:id]) | |
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 Report | |
def initialize | |
@strategy = {} | |
@strategy[Number] = NumberStrategy.new | |
@strategy[String] = StringStrategy.new | |
@strategy[SomethingElse] = SomethingElseStrategy.new | |
end | |
def generate | |
@data = extract_data |
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 Report | |
... | |
def generate | |
@data = extract_data | |
case | |
when @data.are_numbers? | |
make_a_numeric_operation | |
when @data.are_strings? | |
check_grammar | |
when @data.is_something_else? |
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 Report | |
... | |
def generate | |
@data = extract_data | |
case | |
when @data.are_numbers? | |
make_a_numeric_operation | |
when @data.are_strings? | |
check_grammar | |
when @data.is_something_else? |
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 GuaranteedProduct | |
def self.find(id) | |
Product.find(id) || MissingProduct.new | |
end | |
end | |
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| GuaranteedProduct.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, |
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
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id) || MissingProduct.new } | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
#<MissingProduct:>, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.each do |product| | |
puts product.name |
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 Product | |
def name | |
... | |
end | |
end | |
class MissingProduct | |
def name | |
"no product" | |
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
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
puts product.nil? ? '' : product.name |
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
product_ids = [123,456,'',789] | |
products = product_ids.map{|id| Product.find(id)} | |
=> [#<Product:0x000123123 @name="TV"..>, | |
#<Product:0x000123124 @name="Cellphone"..>, | |
nil, | |
#<Product:0x000123125 @name="iPad"..> | |
] | |
products.each do |product| | |
puts product.try(:name) |