This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original Source: https://github.com/chneukirchen/styleguide | |
| = Christian Neukirchen's Ruby Style Guide | |
| You may not like all rules presented here, but they work very well for | |
| me and have helped producing high quality code. Everyone is free to | |
| code however they want, write and follow their own style guides, but | |
| when you contribute to my code, please follow these rules: | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Run this file with `RAILS_ENV=production rackup -p 3000 -s thin` | |
| # Be sure to have rails and thin installed. | |
| require "rubygems" | |
| # We are not loading Active Record, nor the Assets Pipeline, etc. | |
| # This could also be in your Gemfile. | |
| gem "actionpack", "~> 3.2" | |
| gem "railties", "~> 3.2" | |
| # The following lines should come as no surprise. Except by |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ThreadPool | |
| def initialize(size) | |
| @size = size | |
| @queue = Queue.new | |
| @pool = Array.new(@size) do |i| | |
| Thread.new do | |
| Thread.current.abort_on_exception = true | |
| Thread.current[:id] = i | |
| catch(:exit) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'socket' | |
| s = UDPSocket.new | |
| NTP_PACKET_SIZE= 48 | |
| EPOCH_SECONDS = 2208988800 | |
| ntppacket = [0b11100011, 0, 6, 0xEC, 0, 49, 0x4E, 49, 52,0,0,0,0].pack("C4QC4Q4") | |
| puts "Packet size matches: #{ntppacket.bytes.count == NTP_PACKET_SIZE}" | |
| s.send ntppacket, 0, "pool.ntp.org", 123 | |
| response = s.recv(NTP_PACKET_SIZE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # gem 'hashie' | |
| begin | |
| app_name = Rails.application.class.parent_name.downcase | |
| file_path = File.join(Rails.root,'config',"#{app_name}.yml") | |
| app_config = Hashie::Mash.new(YAML.load_file(file_path)).freeze | |
| rescue Errno::ENOENT | |
| raise "Can not find #{app_name.upcase} config file: #{file_path}" | |
| rescue Psych::SyntaxError | |
| Rails.logger.error "Can not parse #{app_name.upcase} config file: #{file_path}:" | |
| puts "Can not parse #{app_name.upcase} config file: #{file_path}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def median_split(arr) | |
| c = arr.size%2 | |
| subset = [arr[0..arr.size/2-1], arr[arr.size/2+c..-1]] | |
| m = median arr | |
| subset[0].push m | |
| subset[1].unshift m | |
| subset | |
| end | |
| def median(arr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| $==$'; | |
| $;||$.| $|;$_ | |
| ='*$ ( ^@(%_+&~~;# ~~/.~~ | |
| ;_);;.);;#) ;~~~~;_,.~~,.* +,./|~ | |
| ~;_);@-, .;.); ~ ~,./@@-__);@-);~~,.*+,. | |
| /|);;;~~@-~~~~;.~~,. /.);;.,./@~~@-;.;#~~@-;; | |
| ;;,.*+,./.);;#;./@,./ |~~~~;#-(@-__@-__&$#%^';$__ | |
| ='`'&'&';$___="````" |"$[`$["|'`%",';$~=("$___$__-$[``$__"| | |
| "$___"| ("$___$__-$[.%")).("'`"|"'$["|"'#"). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "diff/lcs" | |
| require "diff/lcs/hunk" | |
| require 'pp' | |
| class Differ | |
| def initialize(actual, expected) | |
| @actual = actual | |
| @expected = expected | |
| end | |
| def hunks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ALPHABET = ('a'..'z').to_a | |
| code = '2114111925' | |
| class WordTree | |
| attr_accessor :l, :r, :my_letter, :string | |
| def initialize(my_letter, string) | |
| @string = string | |
| @my_letter = my_letter | |
| try_init_childs | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| git branch --merged | grep -v "\*" | xargs -n 1 git branch -d |