Skip to content

Instantly share code, notes, and snippets.

@emaraschio
emaraschio / SOLID.markdown
Last active February 16, 2025 23:43
SOLID Principles with ruby examples

SOLID Principles with ruby examples

SRP - Single responsibility principle

A class should have only a single responsibility.

Every class should have a single responsibility, and that responsibility should be entirely encapsulated. All its services should be narrowly aligned with that responsibility, this embrace the high cohesion.

OCP - Open/closed principle

Software entities should be open for extension, but closed for modification.

@lfbittencourt
lfbittencourt / clear_tags.rb
Last active March 21, 2017 11:03
This Ruby script removes all local and remote tags in a single-line way, so you don't need supply your credentials several times. Optionally, you can remove only tags greater than a specific version.
#!/usr/bin/env ruby
# Only tags >= min_tag will be removed.
min_tag = '0.0.0'
tags = `git tag`.split(/\s+/).select do |t|
Gem::Version.new(t) >= Gem::Version.new(min_tag)
end
`git tag -d #{tags.join ' '}`
`git push origin #{tags.map { |t| ':' + t }.join ' '}`
@demisx
demisx / active_record_objects_autosave.md
Last active July 4, 2024 14:20
When Active Record Child Objects are Autosaved in Rails

belongs_to:

  1. Assigning an object to a belongs_to association does not automatically save the object. It does not save the associated object either.

has_one:

  1. When you assign an object to a has_one association, that object is automatically saved (in order to update its foreign key).
  2. In addition, any object being replaced is also automatically saved, because its foreign key will change too
  3. If either of these saves fails due to validation errors, then the assignment statement returns false and the assignment itself is cancelled.
  4. If the parent object (the one declaring the has_one association) is unsaved (that is, new_record? returns true) then the child objects are not saved. They will automatically when the parent object is saved.
module Foo
class Error < StandardError; end
end
module Foo
class Baz
def initialize
puts Error
end
end
@jwilkins
jwilkins / httparallel.rb
Last active December 28, 2024 15:11
parallel http/https download with typhoeus
#!/usr/bin/env ruby
# 25s curl -o all.gz.curl http://download.openwall.net/pub/passwords/wordlists/all.gz
# 11s for this script with 4 parallel requests
require 'typhoeus'
def pfetch(url, splits=4)
response = Typhoeus.head(url)
parallelize = false
basename = File.basename(url)
size = nil
@techniq
techniq / audit_mixin.py
Created March 16, 2013 01:05
Useful SQLAlchemy Mixins
from datetime import datetime
from sqlalchemy import Column, Integer, DateTime, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declared_attr
from flask_security import current_user
class AuditMixin(object):
created_at = Column(DateTime, default=datetime.now)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
@inkel
inkel / ruby-no-cflags.txt
Created December 1, 2012 12:08
Benchmark for comparison of homebrew's ruby with and without CFLAGS="-march=native -O3"
>> /usr/bin/time -l ruby thebench.rb 30 1000
Rehearsal --------------------------------------------------------
fib(30) 0.260000 0.000000 0.260000 ( 0.255475)
1000 tempfiles 0.120000 0.250000 0.370000 ( 0.429621)
----------------------------------------------- total: 0.630000sec
user system total real
fib(30) 0.260000 0.000000 0.260000 ( 0.259108)
1000 tempfiles 0.130000 0.270000 0.400000 ( 0.848950)
@tkrotoff
tkrotoff / event.rb
Created October 9, 2012 13:45
Rails JSON serialization and deserialization
class Event < ActiveRecord::Base
include EventJSON
attr_accessible *EventJSON.attributes
validates :starts_at, presence: true
validates :ends_at, presence: true
validates :all_day, inclusion: { in: [true, false] }
end
@rgreenjr
rgreenjr / postgres_queries_and_commands.sql
Last active May 8, 2025 09:19
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@czottmann
czottmann / Procfile
Created June 15, 2011 13:43
Example of a Foreman/Capistrano/upstart setup
worker: QUEUE=* bundle exec rake environment resque:work
scheduler: bundle exec rake environment resque:scheduler