Skip to content

Instantly share code, notes, and snippets.

View gazay's full-sized avatar
🌐

Alex Gaziev gazay

🌐
View GitHub Profile
@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\\\'\''
@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)
@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
@JosephPecoraro
JosephPecoraro / shell-execution.rb
Last active March 19, 2025 06:46
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