Skip to content

Instantly share code, notes, and snippets.

class Foo
def some_method
puts "In some_method"
def some_other_method
puts "In some other method"
"Foo"
end
"Bar"
end
end
class DashboardController < ResourceController
def get
# show dashboard
end
end
@rentalcustard
rentalcustard / privacy_example_failing.rb
Created October 30, 2012 08:43
Ruby private methods
class PrivacyExample
def some_public_method
self.some_private_method
end
def some_private_method
"o hai"
end
private :some_private_method
end
@joakimk
joakimk / functional_core_imperative_shell.rb
Last active October 14, 2015 00:27
An example of "functional core, imperative shell" as far as I understand it. For more info about this concept, see https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell.
# Written for this gist, but inspired by production code (I haven't run this code).
# Imperative shell
class Timer
def initialize(record)
@record = record
end
def start
save_changes clock.start
@henrik
henrik / rules.md
Last active May 23, 2022 12:31
Sandi Metz' four rules from Ruby Rogues episode 87. Listen or read the transcript: http://rubyrogues.com/087-rr-book-clubpractical-object-oriented-design-in-ruby-with-sandi-metz/
  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done. And your view can only know about one instance variable.

You can break these rules if you can talk your pair into agreeing with you.

@skwp
skwp / watch_listing.rb
Last active December 15, 2015 18:09
Hexagonal extraction of a controller action from reverb.com, showing reuse between controller and Grape/Roar based API. This is sample code stripped down to the essentials and is not guaranteed to work as-is :)
class Reverb::Actions::WatchListing
def self.watch(user, product, listener)
if product.owner?(user)
listener.failure(I18n.t('flash.watchlist.error_own'))
else
Reverb::Analytics.track(user, :watch_product) # FIXME, this doesn't belong here
user.user_watch_products.create(:product_id => product.id)
listener.success
end
end
@michaelfeathers
michaelfeathers / dentdetect.rb
Last active December 16, 2015 07:08
Attempt to discern number of spaces used for an indent across a set of source files.
class Array
def freq_by &block
group_by(&block).map {|k,v| [k, v.count] }.sort_by(&:first)
end
def freq
freq_by {|e| e }
end
end
@sepastian
sepastian / zebra.rb
Last active January 15, 2018 07:28
Print a label on a Zebra TLP 2844 in Ruby. The printer is EPL/2 capable and connected via USB.
# Print a label on a Zebra TLP 2844 in Ruby.
# The printer is EPL/2 capable and connected via USB.
# EPL/2 reference: http://www.zebra.com/apps/dlmanager?dlp=-227178c9720c025483893483886ea54a70963bb77ca94fcc1d65ce9394326ed960e43d023beba35831d5d9bfc1740296347157b5024977a&c=gb&l=en
# https://github.com/larskanis/libusb
require 'libusb'
# Select the printer by vendor ID and product ID.
# To get a list of connected devices, use 'lsusb'.
# 0a5f:0x000a identifies the Zebra TLP 2844.
#!/usr/bin/env ruby
require "with_args" # TBA
class User
with_args String,
def name=(name)
@name = name
end
@tashian
tashian / README.md
Last active December 4, 2020 17:52
Just-in-time label printing on the Zebra ZP450 using node.js + Easypost + Pusher + cups

This is the script we use at yerdle to print labels from our Rails backend to our Zebra ZP450 printer.

the Zebra

See this blog post for the whole story.