Skip to content

Instantly share code, notes, and snippets.

module Rack
class Request
def subdomains(tld_len=1) # we set tld_len to 1, use 2 for co.uk or similar
# cache the result so we only compute it once.
@env['rack.env.subdomains'] ||= lambda {
# check if the current host is an IP address, if so return an empty array
return [] if (host.nil? ||
/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
host.split('.')[0...(1 - tld_len - 2)] # pull everything except the TLD
}.call
puts "hello joe"
def self.tree(path = nil)
Rails.cache.fetch("folder-tree-#{path}", expires_in: 0) do
folders = Folder.where('path LIKE ?', "#{path}%").all
path_parts = path.to_s.count('/')
local_root = root
local_first_tier = []
folders.each do |folder|
folder.children = folders.select { |f| f.parent_id == folder.id }.sort { |a, b| a.position <=> b.position }
local_root = folder if path == folder.path
@mje113
mje113 / gist:5859525
Created June 25, 2013 15:41
Performance improvement for ActionController::Responder#to_format
# Orignal
module ActionController
class Responder
def to_format
if get? || !has_errors? || response_overridden?
default_render
else
display_errors
end
@mje113
mje113 / ruby-2.1.rb
Last active December 21, 2015 07:48 — forked from haileys/ruby-2.1.rb
class Foo
public def my_public_method
1 + 2
end
protected def my_protected_method
3 + 4
end
def my_private_method
@mje113
mje113 / gist:6769883
Created September 30, 2013 20:36
Jruby error...
NoMethodError (super: no superclass method `request_parameters'):
gems/gems/actionpack-3.2.13/lib/action_dispatch/http/request.rb:237:in `POST'
gems/gems/actionpack-3.2.13/lib/action_dispatch/http/parameters.rb:10:in `parameters'
@mje113
mje113 / gist:6823395
Created October 4, 2013 09:33
Example usage of Couchbase Java SDK 1.2.0 callbacks on an async set (in JRuby).
module Couchbase
class AsyncCallback
include Java::NetSpyMemcachedInternal::OperationCompletionListener
def initialize(callback)
@callback = callback
end
def onComplete(future)
$ sudo dpkg -i couchbase-server-enterprise_2.2.0_x86_64.deb
Selecting previously unselected package couchbase-server.
(Reading database ... 70796 files and directories currently installed.)
Unpacking couchbase-server (from couchbase-server-enterprise_2.2.0_x86_64.deb) ...
libssl1* is installed. Continue installing
Minimum RAM required : 4 GB
System RAM configured : 3145728 kB
Minimum number of processors required : 4 cores
Number of processors on the system : 32 cores
Setting up couchbase-server (2.2.0) ...
# A
100.times do |i|
case [i % 3 == 0, i % 5 == 0]
when [true, true] then puts 'Fizz Buzz'
when [true, false] then puts 'Fizz'
when [false, true] then puts 'Buzz'
else
puts i
end
end
$ ping github.com
PING github.com (192.30.252.131): 56 data bytes
64 bytes from 192.30.252.131: icmp_seq=0 ttl=248 time=88.360 ms
Request timeout for icmp_seq 1
64 bytes from 192.30.252.131: icmp_seq=2 ttl=248 time=91.491 ms
64 bytes from 192.30.252.131: icmp_seq=3 ttl=248 time=91.730 ms
64 bytes from 192.30.252.131: icmp_seq=4 ttl=248 time=86.243 ms
64 bytes from 192.30.252.131: icmp_seq=5 ttl=248 time=89.297 ms
Request timeout for icmp_seq 6
Request timeout for icmp_seq 7