Skip to content

Instantly share code, notes, and snippets.

module Destroyable
def call_destructor(id)
@destructors[id].each do |o|
o.__destroy(id) if o.respond_to? :__destroy
end
@destructors.delete(id)
end
def is_destroyable
@destructors = {}
@e-jambon
e-jambon / ruby-destructor-example.rb
Created June 25, 2016 10:01 — forked from iboard/ruby-destructor-example.rb
Ruby 'Destructor' example.
class Foo
attr_reader :bar
def initialize
@bar = 123
ObjectSpace.define_finalizer( self, self.class.finalize(bar) )
end
def self.finalize(bar)
proc { puts "DESTROY OBJECT #{bar}" }
end
require 'tty'
require 'tty-prompt'
prompt = TTY::Prompt.new
answer = prompt.select("my question") do |item|
item.choice 'One', 'Pika'
item.choice "Two", 'Boo'
end
puts "My answer was : #{answer}"
ItemMenu = Struct.new(:lvl, :description, :command_type, :data) do
def action
puts "data = " + data.inspect
end
def save_to filepath
File.open( filepath, 'w+' ) do |f|
Marshal.dump(self,f)
class String
def to_proc
eval "Proc.new { |*args| args.first#{self} }"
end
end

Design patterns (in Ruby)

Design patterns are just tools that help us constructing a software.

Template Method pattern

In the Template Method pattern, we create a skeletal class and it is basis for various subclasses or concrete classes. Within in the skeletal class, there are abstract methods, which in turn, will be overridden by the methods of subclasses.

Let's take an example of simple payment system,

require 'rails_helper'
RSpec.describe TodosController, :type => :controller do
context "GET index" do
#context "POST create" do
#context "GET show" do
#context "PATCH update" do (or PUT update)
#context "DELETE destroy" do
#context "GET new" do

Rails mailer structure

Your application is growing, and you are starting to have a complex mailing system: notification emails, retention emails, misc user emails, admin emails, etc...

It's time to clean up your mailers !

Existing mailer

You may already have a single mailer, responsible of every emails, like this one:

@e-jambon
e-jambon / tmux.md
Created June 21, 2016 12:53 — forked from andreyvit/tmux.md
tmux cheatsheet

tmux cheat sheet

(C-x means ctrl+x, M-x means alt+x)

Prefix key

The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf:

remap prefix to Control + a

@e-jambon
e-jambon / capybara cheat sheet
Created June 21, 2016 12:52 — forked from zhengjia/capybara cheat sheet
capybara cheat sheet
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')