This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product | |
include Mongoid::Document | |
referenced_in :group | |
end | |
class Group | |
include Mongoid::Document | |
references_many :products, :stored_as => :array, :inverse_of => :group | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Car () { | |
} | |
Car.prototype.extDirectOptions = function(options) { | |
this.extDirectOptions = options; | |
} | |
Generator.prototype.controllerMetadata = function() { | |
// find controller in config.controllersDir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def file_copy(src, dest) | |
File.open(dest, 'wb') { |f| f.puts(File.read(src)) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def [](k) | |
fetch(k) do | |
super[k.intern] || super[k.to_s] | |
end | |
end | |
end | |
{ key: 'EXPECTED'}['key'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer