Skip to content

Instantly share code, notes, and snippets.

@kddnewton
kddnewton / json.rb
Last active October 6, 2023 09:25
JSON parser with pattern matching
require "json"
struct = { "a" => 1, "b" => 2, "c" => [1, 2, 3], "d" => [{ "e" => 3 }, nil, false, true, [], {}] }
source = JSON.dump(struct)
tokens = []
index = 0
until source.empty?
tokens <<
@timvisee
timvisee / falsehoods-programming-time-list.md
Last active May 6, 2024 08:30
Falsehoods programmers believe about time, in a single list

Falsehoods programmers believe about time

This is a compiled list of falsehoods programmers tend to believe about working with time.

Don't re-invent a date time library yourself. If you think you understand everything about time, you're probably doing it wrong.

Falsehoods

  • There are always 24 hours in a day.
  • February is always 28 days long.
  • Any 24-hour period will always begin and end in the same day (or week, or month).
@smoil
smoil / account.rb
Last active March 2, 2020 00:32
Ajax select/multi select in Rails using Select2
class Account < ActiveRecord::Base
def self.search(name = nil)
# search logic
end
end
@jdkanani
jdkanani / notepad.html
Last active April 6, 2024 17:09 — forked from jakeonrails/Ruby Notepad Bookmarklet
This bookmarklet gives you a code editor in your browser with a single click.
data:text/html, <style type="text/css">.e{position:absolute;top:0;right:0;bottom:0;left:0;}</style><div class="e" id="editor"></div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var e=ace.edit("editor");e.setTheme("ace/theme/monokai");e.getSession().setMode("ace/mode/ruby");</script>
<!--
For other language: Instead of `ace/mode/ruby`, Use
Markdown -> `ace/mode/markdown`
Python -> `ace/mode/python`
C/C++ -> `ace/mode/c_cpp`
Javscript -> `ace/mode/javascript`
Java -> `ace/mode/java`
Scala- -> `ace/mode/scala`
@cavalle
cavalle / cohesion-and-big-models.md
Last active December 11, 2015 21:09
Cohesion and Big ActiveRecord Models

Cohesion and Big ActiveRecord Models

One of the problems of big ActiveRecord models is their low cohesion. In Rails we group behaviour around the entities of the domain we're modelling. If we use only ActiveRecord models for that, we'll probably end up with classes full of actions, in most cases, completely unrelated to each other (except for the fact that they act on the same domain entity).

To illustrate the issue let's imagine a big Post AR model of a blog application. In this app, users can add tags to their posts. Since this is the only class with this ability, I decide to put the related logic in the Post model itself. For this, presumably at the beginning of the file, I write a couple of has_many entries for tags and taggings. Then, fifty lines below, along with the rest of scopes, I add one more to find posts by tag name. A couple of hundred lines later, I put a virtual attribute tag_list which will be used to update the associations from a string of tag names separated by commas. Fin

@coffeencoke
coffeencoke / README.md
Last active May 19, 2017 08:02
Use a sub directory as a rails application's url root rails 3, nginx, unicorn

Symlink for static files

Among applying the changes in the other files in this gist, you need to symlink the directory you are serving the app with in the root app's public directory so that NGINX can serve the static files in the sub-app.

For example, if you have an app at /home/coffeencoke/apps/app.coffeencoke.com/current/public as your root app, and you have the sub app served at http://app.coffeencoke.com/admin, then you need to symlink /home/coffeencoke/apps/admin-app.coffeencoke.com/current/public to /home/coffeencoke/apps/app.coffeencoke.com/current/public/admin like so:

ln -s /home/coffeencoke/apps/admin-app.coffeencoke.com/current/public /home/coffeencoke/apps/app.coffeencoke.com/current/public/admin
@DAddYE
DAddYE / image_uploader.rb
Created December 30, 2011 22:57
CarrierWave on the fly resizer (work as dragonfly)
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def thumb(size)
begun_at = Time.now
size.gsub!(/#/, '!')
uploader = Class.new(self.class)
uploader.versions.clear
uploader.version_names = [size]
@dhh
dhh / gist:1014971
Created June 8, 2011 18:09
Use concerns to keep your models manageable
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@igrigorik
igrigorik / ruby-1.9-tips.rb
Created February 3, 2011 17:19
Ruby 1.9 features, tips & tricks you may not know about...
def tip(msg); puts; puts msg; puts "-"*100; end
#
# 30 Ruby 1.9 Tips, Tricks & Features:
# http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
#
tip "Upgrading to Ruby 1.9 is simple: rvm install 1.9.2 && rvm --default 1.9.2"
tip "Ruby 1.9 supports named captures in regular expressions!"
class RegistrationsController < ApplicationController
STEP_VALIDATIONS = [
%w(first_name last_name),
%w(billing_address)
]
def new
@user = User.new
end