Skip to content

Instantly share code, notes, and snippets.

set tabstop=2 shiftwidth=2 expandtab
set autoindent
set smarttab
syntax on
set shell=/bin/bash
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
@localhostdotdev
localhostdotdev / compare-select-pluck.rb
Last active March 18, 2019 12:54
Rails ActiveRecord#select vs #pluck, 5x faster (~20k records, 10 runs each)
class Utils
def self.compare(reports, n: 10)
Benchmark.bm do |x|
reports.each do |report|
if report.is_a?(Proc)
x.report { n.times { report.call } }
else
x.report(report.first) { n.times { report.last.call } }
end
end
@localhostdotdev
localhostdotdev / capybara-selenium-chrome-driver-config.rb
Created March 19, 2019 01:23
Set window size, proxy, no infobars, no notifications, no google sync, no audio, ublock (ad blocker and more), config for selenium/capybara/chromedriver in ruby
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument("--window-size=1440,900")
options.add_argument("--proxy-server=localhost:8080")
options.add_argument("--disable-infobars")
options.add_argument("--disable-notifications")
options.add_argument("--disable-sync")
options.add_argument("--mute-audio")
options.add_extension(
File.expand_path("../tmp/ublock.crx", File.dirname(__FILE__))

iTerm dark + lighter dark blue

@localhostdotdev
localhostdotdev / lazy.rb
Last active April 23, 2019 13:53
Lazy: fetch deep values only when you need to
# Example: Lazy.new(lambda { |id| API::HackerNews.item(id) }, 0)
class Lazy
attr_reader :value
def initialize(function:, value:)
@function = function
@value = value
@cached = nil
@is_cached = false
@localhostdotdev
localhostdotdev / struct.from_hash.rb
Created March 25, 2019 15:02
Struct.from_hash: the best of Struct and OpenStruct combined :)
class Struct
def self.from_hash(attributes)
new(*attributes.keys).new(*attributes.values)
end
end
user = Struct.from_hash(id: 1, name: "?")
user.id # => 1
user.names # => NoMethodError (undefined method `names' for #<struct id=1, name="?">)
class Search
attr_reader :verbose
def initialize(search, verbose: false)
@parts = search.split.map(&:downcase)
@verbose = verbose
end
def self.matches?(search:, record:)
new(search).matches?(record)
@localhostdotdev
localhostdotdev / lazy.rb
Last active March 30, 2019 11:57
e.g. `object = Lazy.new(function: -> (id) { somethingThatFetch(id) }, value: 123)`. then `variable.anything` will load up the object
class Lazy
attr_reader :value
def initialize(function:, value:)
@function = function
@value = value
@cached = nil
@is_cached = false
end
# Hey guys I got a problem with my reviews controller when it comes to post a review, I'm using devise , is a online shop, and To post a review Im using AJAX, but the AJAX does not work
# this is my ReviewsController:
class ReviewsController < ApplicationController
#before_action :set_shirt, only: [:show]
def create
@shirt = shirt_url.find(params[:shirt_id])
Ruby:
type: programming
ace_mode: ruby
codemirror_mode: ruby
codemirror_mime_type: text/x-ruby
color: "#701516"
aliases:
- jruby
- macruby
- rake