Skip to content

Instantly share code, notes, and snippets.

# Module Frequency
# A small dsl written in ruby to work with frequency events (never, sometimes, always..)
#
#
# Alteracoes:
# * removi "if block_given?" e passei &block como parametro para #execute_with_probability
# para remover duplicacao
# * mudei #correct_if_string
module Frequency
NORMALLY = 0.75
class Product
include Mongoid::Document
referenced_in :group
end
class Group
include Mongoid::Document
references_many :products, :stored_as => :array, :inverse_of => :group
end
function Car () {
}
Car.prototype.extDirectOptions = function(options) {
this.extDirectOptions = options;
}
Generator.prototype.controllerMetadata = function() {
// find controller in config.controllersDir
def word_wrap(word, len = 22)
if word.size < len
word
else
new_word = []
parts = word.split(/\s/).map { |p| p.strip }
while part = parts.shift and (new_word.join(" ").size + part.size) < len
new_word << part
end
def file_copy(src, dest)
File.open(dest, 'wb') { |f| f.puts(File.read(src)) }
end
@dlt
dlt / gist:1191511
Created September 3, 2011 17:40
nested_attributes
class Product < ActiveRecord::Base
has_many :fields, :as => :attributable, :dependent => :destroy
accepts_nested_attributes_for :fields
end
class Field < ActiveRecord::Base
belongs_to :attributable, :polymorphic => true
belongs_to :column
scope :with_column, select("fields.*, columns.column_name AS 'name'").joins(:column)
@dlt
dlt / gist:1421359
Created December 2, 2011 01:56
Why is 'y' defined?
ruby-1.9.2-p180 :001 > if 1 then x = 1 else y = 0 end
=> 1
ruby-1.9.2-p180 :002 > y
=> nil
ruby-1.9.2-p180 :003 > x
=> 1
ruby-1.9.2-p180 :004 > z
NameError: undefined local variable or method `z' for main:Object
from (irb):4
from /home/dlt/.rvm/rubies/ruby-1.9.2-p180/bin/irb:16:in `<main>'
saved = 0
not_saved = 0
Product.all.to_a.select do |p|
p.image && p.image.url
end.each do |p|
begin
temp = Tempfile.new("foo")
temp.puts \
Mongo::GridFileSystem.new(Mongoid.database).
@dlt
dlt / gist:1515236
Created December 23, 2011 20:07
whyyyyyyy?
class Hash
def [](k)
fetch(k) do
super[k.intern] || super[k.to_s]
end
end
end
{ key: 'EXPECTED'}['key']
C = Class.new do
def has?(methods)
!!methods.split(".").inject(self) do |object, method|
object.respond_to?(method) && object.send(method)
end
end
end
c = C.new
c.has?("methods") # => true