1 2 3 4
Gender.create([ {:name => "Male", :is_default => true}, {:name => "Female", :is_default => true} ...
Ruby Create database records
1 2 3 4
def validate_permalink(attr_name) if permalink? permalink = sanitize_permalink(attr_name) ...
Ruby Simple if staments
easy one?
1 2 3 4
# In helper def show_row(label, attribute) ...
Ruby Infer column name from inst...
Assume you have a model, li...
1 2 3 4
# Associations # # User: has_many :sources, :as => :sourceable, :dependent => :destroy ...
Ruby Get child elements from a o...
For a object (e.g. user), I...
1 2 3 4
def make_verts(s, r) a = Math::PI/s b = Math::PI/(s/2) ...
Ruby Dynamic Vertices Generation
Can somebody check my math?...
1 2 3 4
def dedent() sRaw = self; sep = "\n"; ...
Ruby ruby dedent whitespace in a...
Remove leading whitespace f...
1 2 3 4
def hash_from_string(string, value) hash, split = {}, string.split(".") (split.size-1).downto(0) do |i| ...
Ruby Nested Hash from delimiter-...
the string can contain 1-n ...
1 2 3 4
File.open( "binary.bin", "wb" ) do |io| obj = BinData::String.new(:initial_value => "Binary file start-point").write(io) obj = BinData::Uint32be.new(:initial_value => 0).write(io) ...
Ruby Creating a binary file with...
Hi, I'm currently using Bin...
1 2 3 4
def flights_by_airline(airline_id, flight_number=nil, date=nil, params={}) if flight_number && date t = convert_date(date) ...
Ruby Better way to offer several...
I'm wrapping an API in ruby...
1 2 3 4
tl = Time.now.localtime (0..356).each {|i| d = (Date.new(tl.year, tl.month, tl.day) +i ); printf("\n%03s\n\n", Date::ABBR_MONTHNAMES[d.month]) if d.day == 1; ...
Ruby Generate Days of a Year
I wrote the following to ge...
1 2 3 4
class String def squish x = 0 ...
Ruby Function to remove consiste...
The code doesn't feel elega...
1 2 3 4
# Initial attributes Hash # attributes = { ...
Ruby Ruby Hash Extraction Using ...
I'm looking for a better wa...
1 2 3 4
class Anagram def self.resolves?(first_word, second_word) ...
Ruby Anagram Solver
This code outputs whether t...
1 2 3 4
def update_materials jm = JobMaterial.find(:first, :conditions => {:material_id => params[:job_material][:material_id], :job_id => params[:job_id], :location => params[:job_material][:location]}) if jm ...
Ruby add or increment item count
This is a custom method in ...
1 2 3
def render_page_sitemap(scope, sitemapname,statics, machines, lang) ...
Ruby Speeding up Sitemap generation
Hi,
I'm looking for a wa...
1 2 3
def encode self[:code] = self[:code].gsub(/\&/, "&").gsub(/\</, "<").gsub(/\>/, ">") end
Ruby Ruby Html Encode
I'm looking for an easier/b...
1 2 3 4
# code_for(0) => 0 # code_for(62) => 10 # code_for(124) => 20 ...
Ruby Create a baseXX string base...
I'm trying to create Bit.ly...
1 2 3 4
@merged << @array1 @merged << @array2 @merged.flatten! ...
Ruby Merging two active record a...
I'm merging two active reco...
1 2 3 4
def self.capture_system(*args) args = args.flatten.compact.collect { |arg| arg.to_s } return false if args.blank? ...
Ruby Silencing current output, b...
Is there a shorter way to d...
1 2 3 4
def score(dice) count = [0,0,0,0,0,0] ...
Ruby Looking for other/better so...
# Greed is a dice game wher...
1 2 3 4
for y in 0...height for x in 0...width # BGR => RGB & miror y ...
Ruby Symplify few loops
I need to reformat .bmp dat...
1 2 3 4
module FancyDates (1..12).each do |mo| t = Time.mktime(1999,mo) ...
Ruby Cleaner way to define const...
This is code that lets you ...
1 2 3
Clip.find_by_sql(["select * from clips where not exists (select 'clip_id' from histories where histories.clip_id = clips.id and histories.user_id = ?) ORDER BY clips.created_at DESC LIMIT 20", user.id])
Ruby ActiveRecord find_by_sql
How can I turn this into so...
1 2 3
def rot_encode(s, offset=2) s.split('').map{ |char| (char.ord + offset).chr }.join end
Ruby rot encode
Rotation encoding with vari...
Can't we create first all r...