Skip to content

Instantly share code, notes, and snippets.

#Refactor following code using singleton pattern and explain your answer
require 'singleton'
class Logger
include Singleton
LOG_FILE_NAME = 'dev.log'
def initialize
# This module helps converting to string every object that includes it
module Stringfier
attr_reader :app
def to_str
"Your app http status is: -> #{@app[0]}<-, " + \
"with headers: ->#{@app[1]}<-, " + \
"and body: ->#{@app[2]}<-"
end
end
test_app = lambda do |env|
[200,
{'Content-Type' => 'text/html',
'Content-Length' => '3'}, ['Yes']]
end
describe test_app, 'RackApp' do
it { should respond_to(:call) }
---
BUNDLE_DISABLE_SHARED_GEMS: "1"
@chischaschos
chischaschos / aliasing.rb
Created November 15, 2010 16:07
rubylearning.org - metaprogramming - method wrapper
module RubyLearning
module Module
WRAPPER_PREFIX = 'wrapper_'
WRAPPED_PREFIX = 'wrapped_'
def before(method, prev_option = {})
prev_option.merge!(:before => normalize_method_name(method))
end
@chischaschos
chischaschos / class1.rb
Created November 16, 2010 19:16
Metaprogramming basics post code
class A
end
puts "--" + A.instance_methods(false).join(', ')
class A
def otro
end
end
@chischaschos
chischaschos / .gitignore
Created November 26, 2010 19:13
A Playfair Cypher ruby example from the ruby core rubylearning.org course
*.swp
@chischaschos
chischaschos / analyzer.rb
Created December 3, 2010 03:15
rubyc example data
headers = nil
rows = []
File.open(ARGV[0] || 'data.csv', 'r') do |file|
headers = file.gets.split(',')
while(line = file.gets) do
cols = line.split(',')
rows << cols
end
We couldn’t find that file to show.
@chischaschos
chischaschos / saludador.rb
Created December 10, 2010 01:59
rubyc rspec intro
class Saludador
def initialize(*user)
@user = user[0]
raise "An user has to be provided" unless @user
end
def saluda
"Hola amigo"
end