Skip to content

Instantly share code, notes, and snippets.

View markbates's full-sized avatar

Mark Bates markbates

View GitHub Profile
@markbates
markbates / abstract_interfaces.rb
Last active December 27, 2015 23:08
Code and slides from my RubyConf 2013 talk, "Mangling Ruby with TracePoint". Slides: http://www.slideshare.net/markykang/mangling-ruby-withtracepoint
require 'singleton'
tracer = TracePoint.new(:end) do |tp|
ancestors = tp.self.ancestors.dup
ancestors.delete(tp.self)
missing_methods = []
ancestors.each do |ancestor|
methods = AbstractMethodManager.instance.methods[ancestor]
if methods
methods.each do |m|
@markbates
markbates / development.txt
Last active December 27, 2015 08:09
I wrote a simple TracePoint program that traces all of the method calls in a simple Rails 4.0.1 "Hello World" application. Here are the results. If you want to see how I wrote this program, come see my RubyConf 2013 talk. :)
TracePoint:
disable 1
enable 1
Object:
require 1983
load_dependency 485
[] 18
hash 15
initialize 6
@markbates
markbates / wtf_constructor.java
Created August 13, 2013 13:22
This is why I don't do Java anymore. Who thought this constructor made sense?
package de.payment.integration.model;
public class ParameterBean
{
private String version;
private String sender;
private String token;
private String response;
private String channel;
private String mode;
defmodule Chop do
import IO, only: [puts: 1]
def guess(actual, range) do
[_..e] = range
q = div(e, 2)
puts "initial guess: #{q}"
think(q, actual, range)
end
@markbates
markbates / gist:5216023
Created March 21, 2013 19:37
Conversation with a recruiter today...
Recruiter: "Hi Mark, I have a great opportunity for you."
Me: "This isn't a great time for me. Thanks."
Recruiter: "When would be a better time?"
Me: "Honestly, never. I'm quite happy where I am."
Recruiter: "Well, that's quite rude. You don't know what I'm about to tell you."
<ul><li name="object" class="hasSubList active"><span class="object-key">object: </span><ul type="invoice"><li name="date" class=""><span class="object-key">date: </span><span class="number">1362081427</span></li><li name="id"><span class="object-key">id: </span><a href="/invoices/in_1NNYz9WbrTvRmU">in_1NNYz9WbrTvRmU</a></li><li name="period_start" class=""><span class="object-key">period_start: </span><span class="number">1359479477</span></li><li name="period_end" class=""><span class="object-key">period_end: </span><span class="number">1362071477</span></li><li name="lines" class="hasSubList"><span class="object-key">lines: </span><ul type="list"><li name="object" class=""><span class="object-key">object: </span><span class="string">"list"</span></li><li name="count" class=""><span class="object-key">count: </span><span class="number">1</span></li><li name="url" class=""><span class="object-key">url: </span><span class="string">"/v1/invoices/in_1NNYz9WbrTvRmU/lines"</span></li><li name="data" class="hasSub
@markbates
markbates / script.md
Created December 21, 2012 18:21
Getting Started with Sinatra

In an earlier video we took a look at Rack to build incredibly lightweight web applications with Ruby. Rack's toolkit allowed us to quickly throw to get a working application, but we did have to put a little effort into it once we wanted to build something a little more complex.

Sometimes you want a fast and simple framework for building a simple web application. Perhaps you only need to respond to a handful of routes, or you want the response time for a small part of a bigger application to be lighting fast. The Sinatra framework is made for just these moments.

Today let's take a quick look at this framework and see how quickly we can build lightweight web applications.

To get started we first need to install the Sinatra gem:

gem install sinatra
@markbates
markbates / gist:4240848
Created December 8, 2012 16:06
Getting Started with Rack

If you're writing web applications with Ruby there comes a time when you might need something a lot simpler, or even faster, than Ruby on Rails or the Sinatra micro-framework. Enter Rack.

Rack describes itself as follows:

Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks.

Before Rack came along Ruby web frameworks all implemented their own interfaces, which made it incredibly difficult to write web servers for them, or to share code between two different frameworks. Now almost all Ruby web frameworks implement Rack, including Rails and Sinatra, meaning that these applications can now behave in a similar fashion to one another.

At it's core Rack provides a great set of tools to allow you to build the most simple web application or interface you can. Rack applications can be written in a single line of code. But we're getting ahead of ourselves a bit.

@markbates
markbates / pry_repl.rb
Created November 15, 2012 19:12
SublimeREPL & Rails
require 'rubygems'
gem 'pry'
require 'pry'
PWD = ENV["PWD"]
env_rb = File.join(PWD, "config", "environment.rb")
FileUtils.cd PWD
if File.exists?(env_rb)
require env_rb
end

Cachley is an add-on for providing simple page caching.

Adding page caching to your application is incredibly important. Slow pages mean higher bounce rates and ultimately less customers and less money for you. If you're pages take over 1 second to render Cachely can help. Cachely can render your pages for you, along with your original status code and headers, in just milliseconds.

Cachely also helps with managing your cached pages. See which pages have been cached and what the HTML for those pages looks like. Expire your cached pages using Regular Expressions. All this and more is just waiting for you. Page caching has never been easier and more powerful.

Cachley is accessible via an API and has supported client libraries for Ruby on Rails/Rack-based applications. Further clients are coming.

Provisioning the add-on