Skip to content

Instantly share code, notes, and snippets.

View patshaughnessy's full-sized avatar

Pat Shaughnessy patshaughnessy

View GitHub Profile
@patshaughnessy
patshaughnessy / gist:3105631
Created July 13, 2012 15:50
Why is for faster than each on Ruby 1.8.7?

Great question! It turns out that in Ruby 1.9 or 2.0 it's actually faster to use each, and not for (by a similar very small amount).

Looking into why for is faster than each in Ruby 1.8.7, it turns out Ruby 1.8.7 uses a slightly different code path for each vs. for, although for still is automatically evaluated as a call to each internally:

if (nd_type(node) == NODE_ITER) {
  result = rb_eval(self, node->nd_iter);
}
else {
  VALUE recv;
The "tostring" instruction below calls into this C code, from string.c. This checks if the expression/object is already a string, and does nothing if it is. If it's not a string, it calls "to_s" on the object.
VALUE
rb_obj_as_string(VALUE obj)
{
VALUE str;
if (RB_TYPE_P(obj, T_STRING)) {
return obj;
}
# Prime factorization.
perl -le '$_= 1 x pop; print $+[1] and s/$1/1/g while /^(11+?)\1*$/' 60
2
2
3
5
@patshaughnessy
patshaughnessy / gist:5672776
Last active December 17, 2015 21:09
Public Speaking Advice
Public speaking advice from a conversation between Ben Orenstein, Jeff Casimir and Katrina Owen.
Listen here: http://learn.thoughtbot.com/podcast/50
- Do remote talks via Skype with user groups or small conferences
- Don't apologize up front about how little you know or how unprepared you are
- Don't introduce yourself - people either know who are you or don't care
- Just start with the problem - why am I listening to this?
- Action right off the bat - like a James Bond movie hook people up front with something interesting
@patshaughnessy
patshaughnessy / gist:8239149
Created January 3, 2014 14:53
x86_64 assembly language generated for MRI rb_obj_not (object.c line 186)
_rb_obj_not: ## @rb_obj_not
.cfi_startproc
Lfunc_begin7:
.loc 1 185 0 ## object.c:185:0
## BB#0:
pushq %rbp
Ltmp90:
.cfi_def_cfa_offset 16
Ltmp91:
.cfi_offset %rbp, -16
@patshaughnessy
patshaughnessy / gist:8239185
Last active January 2, 2016 02:48
x86_64 assembly language generated for MRI rb_obj_not (object.c line 186) with modified RTEST macro
-- Change RTEST:
//#define RTEST(v) !(((VALUE)(v) & ~Qnil) == 0)
#define RTEST(v) ((VALUE)(v) != Qnil && (VALUE)(v))
-- Recompile with -S flag:
clang -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Werror=pointer-arith -Werror=write-strings -Werror=declaration-after-statement -Werror=shorten-64-to-32 -Werror=implicit-function-declaration -Werror=division-by-zero -Werror=extra-tokens -pipe -D_FORTIFY_SOURCE=2 -fstack-protector -fno-strict-overflow -fvisibility=hidden -DRUBY_EXPORT -fPIE -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -D_DARWIN_UNLIMITED_SELECT -D_REENTRANT -I. -I.ext/include/x86_64-darwin12.0 -I./include -I. -S -o object.o -c object.c
-- And here's the new assembly language - it's longer with a condition in the middle (See: je LBB7_2)
# Prepare a new stack frame (save the old base pointer, and set
the new base pointer to the current stack pointer)
pushq %rbp
movq %rsp, %rbp
# Get the address of your string (lea = load effective address)
# and put it into the rdi register as a parameter to printf.
leaq L_.str(%rip), %rdi
# Not sure why this is needed - I believe it sets eax to zero.
@patshaughnessy
patshaughnessy / application.css.scss
Created November 23, 2011 20:39
application.css.scss example overriding Twitter Bootstrap variables (using bootstrap-sass)
/*
* application.css.scss
*= require_self
*/
@import "bootstrap/reset.css.scss";
@import "bootstrap/variables.css.scss";
// Override link color for my app:
@patshaughnessy
patshaughnessy / ruby-book-club-questions.md
Last active August 18, 2017 19:38
Ruby Book Club Questions - Episodes 7 and 8

Hi guys, So in episode 7 you were asking about where and how the puts method connects to the computer’s display and writes the output. And in episode 8 you were asking about the times method, how that worked and why it wasn’t implemented with YARV instructions. In both cases, I didn’t go into detail about this in the book because it would have distracted you from the topic at hand, which is how YARV executes your code. (Or in this case, my example Ruby code from the book.)

Both the puts and times methods were written by the Ruby code team, and not by you or me. Both of them are implemented in C code. So when you’re writing a Ruby program, some of the methods you write yourself, but you get many other methods for free because they are part of the Ruby language. Many of the these built in methods are part of the standard library, which means they are Ruby code written by the Ruby core team, while other built in methods are written directly in C code by the Ruby team.

As you know, the puts method tak

@patshaughnessy
patshaughnessy / main.rs
Created June 9, 2018 14:42
Execute a simple SQL report using Rust and Diesel
#[macro_use]
extern crate diesel;
use diesel::prelude::*;
table! {
users (id) {
id -> Int4,
first_name -> Nullable<Varchar>,
last_name -> Nullable<Varchar>,