Skip to content

Instantly share code, notes, and snippets.

View jack-nie's full-sized avatar
🎯
Focusing

jack-nie

🎯
Focusing
View GitHub Profile
@jack-nie
jack-nie / movie-titles.rb
Created July 4, 2016 12:17 — forked from karmi/movie-titles.rb
Multiple analyzers and query fields in Elasticsearch for auto-completion
require 'tire'
# Tire.configure { logger STDERR, level: 'debug' }
Tire.index('movie-titles') do
delete
create \
settings: {
index: {
analysis: {
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Returns the result of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111
@jack-nie
jack-nie / 1_create_products.rb
Created April 15, 2016 11:31 — forked from hakanensari/1_create_products.rb
Sequel + uuid-ossp
Sequel.migration do
up do
run 'CREATE EXTENSION "uuid-ossp"'
create_table :products do
column :id, :uuid, :default => Sequel.function(:uuid_generate_v4), :primary_key => true
end
end
end
@jack-nie
jack-nie / stacked_middlewares.rake
Created April 9, 2016 09:28 — forked from tyabe/stacked_middlewares.rake
Display lists of all stacked rack middleware for your padrino app
##
# This is rake task of padrino-framework
# Display lists of all stacked rack middleware for your padrino app.
# distributed under the MIT License(http://tyabe.mit-license.org/)
#
def stacked_middlewares(app, args)
require Padrino.root('config/boot.rb')
app_obj = app.app_obj
instance = app_obj.new!
build = app_obj.build(instance)
@jack-nie
jack-nie / array_to_proc.rb
Last active September 10, 2015 05:20 — forked from geowy/array_to_proc.rb
Array#to_proc, shorthand for creating a block that selects elements of an array/hash/collection.
class Array
# Returns a proc which calls [] with the array's contents as arguments
#
# ====Usage
# [[1, 2], [3, 4], [5, 6]].map(&[0])
# # => [1, 3, 5]
#
# [{ hello: 'world' }, { hello: 'sun', goodbye: 'moon' }].map(&[:hello])
# # => ['world', 'sun']
@jack-nie
jack-nie / ova.rb
Last active September 10, 2015 05:20 — forked from 0x0dea/ova.rb
"Real" method overloading in Ruby!
module Ova
# autovivifying map from [class][method][signature] to Method
@@Ova = Hash.new { |h, k| h[k] = Hash.new(&h.default_proc) }
# Wrap #respond_to? for parity with Class#===.
Responder = Struct.new(:method) do
def === obj
obj.respond_to?(method)
end
end