Skip to content

Instantly share code, notes, and snippets.

View mayra-cabrera's full-sized avatar
💃
Nothing to do here. Go to https://gitlab.com/mayra-cabrera instead

Mayra Cabrera mayra-cabrera

💃
Nothing to do here. Go to https://gitlab.com/mayra-cabrera instead
View GitHub Profile
- if @post.is_recent?
= image_tag(@post.image)
class Post < ActiveRecord::Model
def recent?
published_at > 2.days.ago
end
end
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
end
class Report
def initialize
@strategy = {}
@strategy[Number] = NumberStrategy.new
@strategy[String] = StringStrategy.new
@strategy[SomethingElse] = SomethingElseStrategy.new
end
def generate
@data = extract_data
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?
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?
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"..>,
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
class Product
def name
...
end
end
class MissingProduct
def name
"no product"
end
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
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)