Skip to content

Instantly share code, notes, and snippets.

View gazay's full-sized avatar
🌐

Alex Gaziev gazay

🌐
View GitHub Profile
@JosephPecoraro
JosephPecoraro / shell-execution.rb
Last active September 28, 2025 09:58
Shell Execution in Ruby
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Synchronous (blocking)
# Returns the output of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111
@netzpirat
netzpirat / embed.rb
Created March 29, 2011 14:12
ActiveRecord embedding
module ActiveRecord
# Allows embedding of ActiveRecord models.
#
# Embedding other ActiveRecord models is a composition of the two
# and leads to the following behaviour:
#
# - Nested attributes are accepted on the parent without the _attributes suffix
# - Mass assignment security allows the embedded attributes
# - Embedded models are destroyed with the parent when not appearing in an update again
@teeparham
teeparham / active_record_caching.rb
Created May 27, 2011 04:56 — forked from ahoward/active_record_caching.rb
simple active record caching
# for some reason active record doesn't include a simple way to serialize to
# cache. this simple trick will take you far
#
# usage:
#
# user = User.find(id)
#
# key = user.to_cache
#
# user = User.from_cache(key)
@brainopia
brainopia / gist:1255687
Created October 1, 2011 06:28
The easiest way to escape nested bash commands
def bash_escape(argument)
"$'" + argument.gsub("\\'", '\\\\\&').gsub("'", "\\\\'") + "'"
end
ruby_code = "puts 'something'"
command = "ruby -e #{bash_escape ruby_code}"
puts "ssh server #{bash_escape command}" # => ssh server $'ruby -e $\'puts \\\'something\\\'\''
module Lock
extend self
delegate :connection, :to => ActiveRecord::Base
def sync(*args)
lock = Zlib.crc32 args.join
obtain lock
yield
ensure
release lock
@brainopia
brainopia / gist:1379471
Created November 19, 2011 22:39
initial draft of capistrano benchmarker
require 'benchmark'
module Capistrano::Benchmark
def self.included(base)
base.class_eval do
alias execute_task_without_benchmark execute_task
alias execute_task execute_task_with_benchmark
end
end
class @SortableTable
constructor: (table, options) ->
@table = $(table).children('tbody')
@columnSelector = options.columnSelector || 'th'
@iconSelector = options.iconSelector
@rowMapper = options.rowMapper
@afterSort = options.afterSort
@enable()
getColumn: (element) ->
@burke
burke / 0-readme.md
Created January 27, 2012 13:44 — forked from funny-falcon/cumulative_performance.patch
ruby-1.9.3-p327 cumulative performance patch for rbenv

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

# "python-like" import
load path, true # => will load new constants and methods under anonymous module
Module.nesting.first # => inside loaded file will return the current namespace
###
module PythonImport
def import(path)
load 'importer.rb', true
@brainopia
brainopia / gist:2215241
Created March 27, 2012 11:47
Set bom recursively to ruby files
#!/usr/bin/env ruby
require 'epath'
bom = "\xEF\xBB\xBF".force_encoding('binary')
Path.glob('**/*.rb') do |file|
next if file.binread(3) == bom
previous = file.binread
file.write bom + previous
end