Skip to content

Instantly share code, notes, and snippets.

View ebouchut's full-sized avatar

Eric Bouchut ebouchut

View GitHub Profile
@ebouchut
ebouchut / start_with_js
Created July 11, 2014 13:56
Test if a string start with another string in Javascript
prefix = "http://";
url = "http://example.com";
url.substring(0, prefix.length) === prefix; // true
@ebouchut
ebouchut / netcat_listen_on_port
Created September 12, 2014 09:30
netcat listen on port 12345
nc -kl 12345 # netcat listen on port 12345 and keep-alive
@ebouchut
ebouchut / stub_a_rails_controller_method_in_a_test
Created September 15, 2014 14:08
Stub a method of a Rails Controller in a test
describe MyController do
before do
# ...
# Stub MyController.action to always return OK
def @controller.action
render status: 200, nothing: true
end
@ebouchut
ebouchut / rails_i18n
Last active August 29, 2015 14:06
Rails I18N (activemodel errors, simple_form labels and hints)
en:
# ~~~~~~~~
# Model Errors
# ~~~~~~~~
activemodel:
errors:
models:
bidrequest:
not_json: "Not a valid JSON"
url_not_valid: "Not a valid URL"
@ebouchut
ebouchut / rails_controller_cache_buster_xhr_request
Created October 17, 2014 10:19
Add a cache buster in a Rails controller action only for XHR requests
class MyController < ApplicationController
def action_xyz
if request.xhr?
set_cache_buster
end
render json: {what: 'ever'}
end
@ebouchut
ebouchut / rake_task
Created October 28, 2014 14:01
Rake Task
namespace :time_travel do
desc "Description of the lightning task"
task :lightning do |t|
# ...
end
desc "Description of the de_lorean task"
task :de_lorean do |t|
# ...
@ebouchut
ebouchut / ruby_benchmark
Created October 30, 2014 15:24
Ruby Benchmark
require "benchmark"
array = [...]
Benchmark.bm(7) do |x|
x.report("map") { array.map(&:upcase) }
x.report("map!") { array.map!(&:upcase) }
end
@ebouchut
ebouchut / ruby_block_to_proc
Created November 5, 2014 10:28
Convert a block into a Proc in Ruby
def convert_to_proc(&proc)
proc
end
hello = convert_to_proc { |name| puts "Hello #{name}" }
hello.call("Eric") # => "Hello Eric"
@ebouchut
ebouchut / git_push_default
Last active August 29, 2015 14:09
git push.default: Which branch(es) to (implicitely) push by default
git config `push.default`:
– `nothing`: do nothing (make the user say what they mean)
– `matching`: push all local branches for which a remote branch of the same name exists
– `upstream`: push only the current branch, push it to its upstream, making push and pull symmetric
– `simple`: like upstream, but only if the branch names match (will become default in 2.0)
– `current`: push just the current branch
@ebouchut
ebouchut / find_xargs_file_with_name_containing_space
Created November 21, 2014 10:22
Remove files whose name contain a space using find and xargs
# I use find -print0 and xargs -0 when the filenames contain characters used as delimitors,
# like space in this case
#
find '/tmp/temp results' -name '*.tmp' -print0 | xargs -0 -n 1 -J % rm '%'