Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
JoshCheek / rails_test_ip_address.rb
Created October 30, 2015 01:36
How to set the IP address for a test on a Rails app
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def index
render text: "Your IP is #{request.remote_ip}"
end
end
# test/controllers/users_controller_test.rb
class UsersControllerTest < ActionController::TestCase
test "should get index" do
@JoshCheek
JoshCheek / touch.c
Created September 18, 2011 05:51
source code for Gnu's touch command
/* touch -- change modification and access times of files
Copyright (C) 1987, 1989-1991, 1995-2005, 2007-2011 Free Software
Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
@JoshCheek
JoshCheek / ruby-command-line-fu.md
Last active September 13, 2024 20:26
Ruby command line fu

Ruby in place of sed / awk

One of Ruby's major influences was Perl, leaving well equipped to replace bash, sed, and awk. Lets take a look at how to do that.

The -e flag

@JoshCheek
JoshCheek / pipeline.rb
Created December 10, 2016 22:36
A shell pipeline in Ruby
# Read initial standard input from the data segment at the end of the file
# You can also see what is ultimately output down there
pos = DATA.pos # => 1617
$stdin.reopen __FILE__ # => #<IO:/var/folders/7g/mbft22555w3_2nqs_h1kbglw0000gn/T/seeing_is_believing_temp_dir20161210-36802-czciw7/program.rb>
$stdin.seek pos, :SET # => 0
# The commands that will be piped together
commands = [
%w[cat],
%w[tr a A],
@JoshCheek
JoshCheek / uninstall_snap_camera_mac_osx.sh
Last active August 31, 2024 10:19
How to uninstall Snap Camera on Mac OS X
# these are reconstructed from a shell session without runnig them, so make
# sure you check that it's a sane thing to do before running it, I make no
# guarantees of fitness, and accept no liability. Run at your own risk.
sudo launchctl remove com.snap.SnapCameraRemover
rm -r ~/Library/Caches/Snap/
rm -r ~/Library/Caches/com.snap.SnapCamera/
rm -r ~/Library/Preferences/Snap/
rm ~/Library/Preferences/com.snap.SnapCamera.plist
rm ~/Library/Preferences/com.snap.Snap\ Camera.plist
sudo rm -rf /Applications/Snap\ Camera.app/
@JoshCheek
JoshCheek / Readme.md
Last active July 29, 2024 14:24
RSpec crash course.
  • specs go in the "spec" directory
  • specs end in "_spec.rb"
  • toplevel begins with describe
  • inside of a describe you have an it
  • example is an alias for it
  • inside of it blocks, you write your code and assertions
  • run with $ rspec or $ rspec spec/some_spec.rb
  • if you need common setup (e.g. add another dir to the load path), put into "spec/spec_helper.rb" and then `require "spec_helper" at the top of the spec
  • using let will allow you to name a value that you want to call from your spec,
@JoshCheek
JoshCheek / api_only_rails_example.rb
Last active July 17, 2024 08:23
How to configure API only Rails (esp how to deal with param parsing).
# I submitted a bug report https://github.com/rails/rails/issues/34244
# b/c Rails was not honouring my `rescue_from` block which was causing my API to
# be inconsistent. I was told this is expected behaviour. I think it's probably
# fine for an HTML app where you control the form inputs. But for an API app,
# the API is public facing and very important, so Rails shouldn't have its own
# special errors that bypass my app's configuration and make my API inconsistent.
#
# Decided it shouldn't be too difficult to handle this myself. So, here is my
# solution. It contains most of the important lessons I've learned about how to
# get a Rails API app setup. It removes Rails' params parsing and adds its own.
@JoshCheek
JoshCheek / ruby_2_and_3_keyword_args.rb
Created July 31, 2021 03:48
Ruby 2 & 3's keyword args and how `ruby2_keywords` works
# Helpers to make the code below easier to read
class Array
def keywords?() = Hash.ruby2_keywords_hash?(self[-1])
def to_keywords!() = (self[-1] = Hash.ruby2_keywords_hash(self[-1]))
end
# Code to facilitate the examples below
def record_args(*args) = args
def call_this(val:) = val
@JoshCheek
JoshCheek / 1-fizz_buzz.rb
Created December 11, 2011 18:47
Ruby Code Golf solutions
puts Solution.new('1. Fizz Buzz', <<SOLUTION)
def fizzbuzz(n)(n%15==0?'FizzBuzz':n%3==0?'Fizz':n%5==0?'Buzz':n).to_s end
SOLUTION
.test { fizzbuzz 3 }.expect { |result| result == "Fizz" }
.test { fizzbuzz 10 }.expect { |result| result == "Buzz" }
.test { fizzbuzz 45 }.expect { |result| result == "FizzBuzz" }
.test { fizzbuzz 31 }.expect { |result| result == "31" }
@JoshCheek
JoshCheek / example_of_postgresql_ltree.rb
Last active January 23, 2024 13:12
Example of PostgreSQL's ltree
require 'pg'
db = PG.connect dbname: 'josh_testing'
db.exec 'begin'
def db.exec(*)
super.to_a
rescue
$!.set_backtrace caller.drop(1)
raise
end