Skip to content

Instantly share code, notes, and snippets.

View gazay's full-sized avatar
🌐

Alex Gaziev gazay

🌐
View GitHub Profile
@gazay
gazay / ipv4_and_ipv6_regexp.rb
Created October 15, 2011 14:23
IPv4 and IPv6 regexp by jteeuven & Chetan Hanumantha
# IP v4 and v6 (with compression) validation regexp
# http://regexlib.com/REDetails.aspx?regexp_id=1000
# http://regexlib.com/REDetails.aspx?regexp_id=2685
VALID_IP = %r{
(^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9]{1,2})){3}$) | # ip v4
(^(
(([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}) | # ip v6 not abbreviated
(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4}) | # ip v6 with double colon in the end
(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4}) | # - ip addresses v6
@gazay
gazay / gist:1520922
Created December 26, 2011 10:47
Marshaling problem
rails 3.1.1:
http://dl.dropbox.com/u/12274059/rails_issue311
rails 4.0.0beta:
http://dl.dropbox.com/u/12274059/rails_issue400beta
@gazay
gazay / cache_helper.rb
Created December 28, 2011 04:20
Creating Fragment class for controller caching
# rails/actionpack/lib/action_view/cache_helper.rb
private
# TODO: Create an object that has caching read/write on it
def fragment_for(name = {}, options = nil, &block) #:nodoc:
if fragment = controller.find_fragment(name, options)
fragment.read
else
# VIEW TODO: Make #capture usable outside of ERB
# This dance is needed because Builder can't use capture
#!/usr/bin/env ruby
require 'epath'
bom = "\xEF\xBB\xBF".force_encoding('binary')
Path.glob('**/*.rb') do |file|
next if file.binread(3) == bom
previous = file.binread
file.write bom + previous
end
@gazay
gazay / gist:2629683
Created May 7, 2012 18:57
Realisations of Range#=== in ruby18 and ruby19
# implementation of Range#=== in 1.8.7 - it was the same method as Range#include?:
<<-EOS
static VALUE
range_include(range, val)
VALUE range, val;
{
VALUE beg, end;
beg = rb_ivar_get(range, id_beg);
end = rb_ivar_get(range, id_end);
@gazay
gazay / proxy.rb
Created October 5, 2012 05:47
Simple proxy server
# Based on https://gist.github.com/74107 by # Copyright (C) 2009 Torsten Becker <torsten.becker@gmail.com>
#
# Rewrited by gazay
require 'socket'
require 'uri'
class Proxy
attr_accessor :socket
@gazay
gazay / client
Created October 7, 2012 04:19
Heroku proxy
#!/usr/bin/env ruby
client_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'proxy'))
$LOAD_PATH.unshift(client_dir) unless $LOAD_PATH.include?(client_dir)
require 'client'
# Get parameters and start the server
if ARGV.size == 2
port, proxy = ARGV
port = port.to_i
@gazay
gazay / helper.rb
Created October 10, 2012 16:59
Autocreating heroku app
require 'fileutils'
class Helper
attr_accessor :dir, :temp_dir
attr_reader :template_path, :original_dir
def initialize(dir = nil)
@template_path = File.expand_path('../../app_template', __FILE__)
@original_dir = FileUtils.pwd
@gazay
gazay / raw
Created December 16, 2013 15:43
"require 'spec_helper'\n\ndescribe 't34' do\n let(:target) {\n\"class X\n def test_method(arg1)\n end\nend # X\"\n }\n\n let(:target2) {\n\"class X\n def test_method(xxx, arg2)\n end\nend # X\"\n }\n\n let(:source) {\n\"class X\n def test_method(arg1, arg2)\n end\nend\"\n }\n\n let(:rewriter) {\n T34::Rewriter.new source\n }\n\n it 'finds methods' do\n expect(rewriter.methods(:test_method)).to be_kind_of Array\n end\n\n it 'finds method nodes' do\n expect(rewriter.methods(:test_method).map(&:class).compact).to eq [T34::Rewriter::API::MethodNode]\n end\n\n it 'manipulates methods' do\n res = rewriter.methods(:test_method) do |method_node|\n method_node.args = method_node.args[0...-1]\n end\n expect(res[0].args.size).to eq 1\n end\n\n it 'manipulates arguments by name' do\n res = rewriter.methods(:test_method) do |method_node|\n method_node.args = method_node.args.select { |it| it.name != 'arg2' }\n end\n expect(res[0].args.size).to eq 1\n expect(rewr
import System.Random (newStdGen, randomR)
import Gimlh
import Data.List.Split (splitOn)
import Data.List (intercalate)
import Control.Monad.State
main = putStrLn $ runFaker $ do
n <- name
return $ unwords ["Hello", "my", "name", "is", n]