Skip to content

Instantly share code, notes, and snippets.

@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
@mboeh
mboeh / rtdocumentation.rb
Created March 19, 2012 19:31
Runtime documentation for Ruby classes
# NOTE: I just slapped this together as a proof of concept. Thread safety etc. is left to the reader.
module RTDocumentation
def self.included(into)
# method_added doesn't seem to work if mixed in
into.send(:define_method, :method_added) do |name|
if @_rtdoc_nextdoc
@_rtdocs ||= {}
@_rtdocs[name.to_s] = @_rtdoc_nextdoc
@mboeh
mboeh / snatcher.rb
Created January 9, 2013 04:15
Gillian, please use extreme caution.
class Snatcher < BasicObject
C = ::Object.new
class << C
def abduct_method(meth, arity)
ObjectSpace.each_object(Module) do |mod|
next if mod.kind_of? Class
if mod.instance_methods.include?(meth)
return mod.instance_method(meth)
end
@kinopyo
kinopyo / gist:5682347
Last active March 21, 2018 05:12
Ruby: to_s vs. to_str

Concept

(The blockquote style does not look so well so I just pasted directly, but these are all quoted from the links in the bottom of this page)

You should not implement to_str unless your object acts like a string, rather than just having a string representation. The only core class that implements to_str is String itself.

[to_i and to_s] are not particularly strict: if an object has some kind of decent representation as a string, for example, it will probably have a to_s method… [to_int and to_str] are strict conversion functions: you implement them only if you object can naturally be used every place a string or an integer could be used.

to_str is used by methods such as String#concat to convert their arguments to a string. Unlike to_s, which is supported by almost all classes, to_str is normally implemented only by those classes that act like strings. Of the built-in classes, only Exception and String implement to_str

@willurd
willurd / web-servers.md
Last active April 20, 2025 00:42
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000
@nellshamrell
nellshamrell / Beneath the Surface: Embracing the True Power of Regular Expressions in Ruby
Last active December 3, 2017 16:35
Resources I consulted when preparing my presentation "Beneath the Surface: Embracing the True Power of Regular Expressions in Ruby"
All of these resources were extremely valuable as I researched regex, finite state machines, and regex in Ruby. Check them out, they're full of fantastic information!
Articles
"Exploring Ruby's Regular Expression Algorithm" by Pat Shaughnessy
http://patshaughnessy.net/2012/4/3/exploring-rubys-regular-expression-algorithm
"Finite State Machines and Regular Expressions" by Eli Bendersky
http://www.gamedev.net/page/resources/_/technical/general-programming/finite-state-machines-and-regular-expressions-r3176
"Regular Expression Matching Can Be Simple and Fast" by Russ Cox
@rjfranco
rjfranco / homebrew-postgresql-update-steps.md
Last active March 4, 2017 23:38
Upgrade steps for homebrew postgresql database

Upgrading a PostgreSQL Database

When using homebrew, on OSX

  • launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist (old way)
  • brew services stop postgresql (new way)
  • mv /usr/local/var/postgres /usr/local/var/postgres.old
  • initdb /usr/local/var/postgres -E utf8
  • pg_upgrade -b /usr/local/Cellar/postgresql/9.3.5_1/bin -B /usr/local/Cellar/postgresql/9.4.0/bin -d /usr/local/var/postgres.old -D /usr/local/var/postgres
  • ./delete_old_cluster.sh (automatically generated in current directory)
  • rm delete_old_cluster.sh
@nellshamrell
nellshamrell / gSchool Regex Resources
Last active March 25, 2016 21:49
Regex Resources for gSchool
Beginning Regex
Intro to Regular Expressions by Michael Fitzgeral
http://www.amazon.com/Introducing-Regular-Expressions-ebook/dp/B008K9OGDA/ref=sr_1_2?ie=UTF8&qid=1374171971&sr=8-2&keywords=Regular+Expressions
Using Regular Expressions in Ruby: Part 1 by Nell Shamrell
https://www.bluebox.net/insight/blog-article/using-regular-expressions-in-ruby-part-1-of-3
Intermediate Regex
@chaitanyagupta
chaitanyagupta / _reader-macros.md
Last active April 19, 2025 05:13
Reader Macros in Common Lisp

Reader Macros in Common Lisp

This post also appears on lisper.in.

Reader macros are perhaps not as famous as ordinary macros. While macros are a great way to create your own DSL, reader macros provide even greater flexibility by allowing you to create entirely new syntax on top of Lisp.

Paul Graham explains them very well in [On Lisp][] (Chapter 17, Read-Macros):

The three big moments in a Lisp expression's life are read-time, compile-time, and runtime. Functions are in control at runtime. Macros give us a chance to perform transformations on programs at compile-time. ...read-macros... do their work at read-time.

@zloedi
zloedi / roguelike_FOV.c
Last active May 13, 2024 21:51
Efficient roguelike grid FOV / shadowcasting / line of sight in a single C function inspired by https://docs.microsoft.com/en-us/archive/blogs/ericlippert/shadowcasting-in-c-part-one
// Copyright (c) 2021 Stoiko Todorov
// This work is licensed under the terms of the MIT license.
// For a copy, see https://opensource.org/licenses/MIT.
// What this function does:
// Rasterizes a single Field Of View octant on a grid, similar to the way
// field of view / line of sight / shadowcasting is implemented in some
// roguelikes.
// Uses rays to define visible volumes instead of tracing lines from origin
// to pixels.