Skip to content

Instantly share code, notes, and snippets.

@fgarcia
fgarcia / gist:80449b6d2754e66f557d
Last active August 29, 2015 14:14
Hide all Virtus attributes
require 'virtus'
module PersonAttributes
include Virtus.module
attribute :name, String
attribute :age, Integer
end
class PersonModel
class Foo
def self.macros(&block)
mod = Module.new
mod.module_eval do
ANSWER = 42
end
self.const_set(:Macros, mod)
Macros.module_eval do
module_eval &block
end
@fgarcia
fgarcia / gist:2d386a6ddd273229d113
Created January 7, 2015 07:57
Can you create too many objects?
Listening to Ruby Rogues I heard one of the panels claiming performance problems because too many objects where created on every request:
http://devchat.tv/ruby-rogues/186-rr-the-4-rules-of-simple-design-with-corey-haines
(Search for: immutability)
I was curious since I started using DDD patterns the number of classes and temporal objects have exploded.
The speaker mentions "a ton of new objects for every request", so I assume that "a ton" is about hundreds or thousand of objects within a large loop, long running loop, nothing to be concerned if it is just a linear sequence of actions for a response.
@fgarcia
fgarcia / gist:189e2dc221fc51084780
Last active August 29, 2015 14:12
generic array adapter
class ArrayAdapter
def initialize(array, &block)
raise 'Adapter is useless without a block' unless block_given?
@raw = array
@transformer = block
end
def each(&block)
# StackOverflow
# http://stackoverflow.com/q/27798855/253098
@fgarcia
fgarcia / gist:b1020381c1077a28e0da
Created October 31, 2014 07:48
Vim #ruby syntax to highlight sets of keywords
""""""""""""""""""""""""""""""""""""""""
" Keywords
" important, more eye catching
syn keyword rubyTodo DANGER WARNING WAITING TODO contained
hi rubyTodo guifg=magenta ctermfg=magenta gui=bold
" less important, less striking
syntax keyword rubySharpBang SOURCES NOTE FUTURE DFS contained
hi rubySharpBang guifg=darkmagenta ctermfg=darkmagenta gui=bold
@fgarcia
fgarcia / a.rb
Created October 29, 2014 08:04
Test if file extension matters in Ruby 'require'
require './b'
require './b.rb'
@fgarcia
fgarcia / gist:b108d06ec954bf613876
Created September 25, 2014 08:36
#rake tasks for #sprockets
require 'sprockets/../rake/sprocketstask'
RACK_ENV = ENV['RACK_ENV'] ||= ENV['RACK_ENV'] ||= 'production' unless defined?(RACK_ENV)
SPROCKETS='production'
Rake::SprocketsTask.new do |t|
manifest_path = File.expand_path('../public/assets/manifest.json', __FILE__)
t.environment = SPROCKETS
t.output = File.expand_path('../public/assets', __FILE__)
t.manifest = Sprockets::Manifest.new(SPROCKETS, manifest_path)
t.assets = %w(application.js application.css)
@fgarcia
fgarcia / Gruntfile.js
Created September 3, 2014 14:40
Plain livereload watcher for Grunt
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
files: 'app/views/**/*.erb',
options: {
livereload: true,
}
}
source 'https://rubygems.org'
#gem "rack-livereload", :group => :development
# Prevent Mac problems
gem "rb-fsevent"
gem "rb-readline"
gem "guard-livereload"
#gem "guard-spring", :group => :development
❯ curl http://127.0.0.1:3000
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>