Skip to content

Instantly share code, notes, and snippets.

View kwilczynski's full-sized avatar
:octocat:

Krzysztof Wilczyński kwilczynski

:octocat:
View GitHub Profile
(main):001:0> [1, 2, 3] <=> [4, 5, 6]
=> -1
(main):002:0> [1, 2, 3] <=> [1, 2, 3]
=> 0
(main):003:0> [8, 7, 3] <=> [4, 5, 6]
=> 1
(main):004:0>
(main):001:0> {:a => 1, :b => 10, :z => 5}.values.max
=> 10
(main):002:0> {:a => 1, :b => 10, :z => 5}.max { |a, b| a[1] <=> b[1] }
=> [:b, 10]
class Server
def initialize
@server = nil
end
def start
@server = TCPServer.new(0)
end
def stop
@kwilczynski
kwilczynski / example.rb
Last active March 21, 2021 23:32
Get Twitter timeline in Ruby
module Twitter
def self.get_timeline(screen_name, query = {})
response = HTTParty.get(
"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=#{screen_name}",
:query => query,
:format => :json
)
response.collect { |t| Hashie::Mash.new(t) }
end
end
@player_state.collect! do |i|
{
:p_name => i.shift,
:balance => i.shift,
:bet => i.shift,
:cards => i.collect! { |c| "#{@cr[c / 16]}#{@cs[c % 16]}" }
}
end
@kwilczynski
kwilczynski / static_fact.rb
Created February 18, 2011 00:36
facts.txt with ability to include other files ...
require 'thread'
require 'facter'
class StaticFact
@@facts = {}
@@mutex = Mutex.new
@@maximum_recursion_level = 4 # Sane default? Hope so ...
@@current_recursion_level = 0
class << self
@kwilczynski
kwilczynski / unique_crontab_minutes.rb
Created March 12, 2011 23:31
Select unique minutes for a crontab job based on the job and host name ...
require 'md5'
#
# unique_crontab_minutes.rb
#
module Puppet::Parser::Functions
newfunction(:unique_crontab_minutes, :type => :rvalue) do |arguments|
raise(Puppet::ParseError, "Wrong number of arguments " +
@kwilczynski
kwilczynski / persistent_crontab_minutes.rb
Created March 13, 2011 13:22
Select minutes for a crontab job based on the job and host name and make the value persistent
#
# persistent_crontab_minutes.rb
#
require 'md5'
module Puppet::Parser::Functions
newfunction(:persistent_crontab_minutes, :type => :rvalue) do |arguments|
raise(Puppet::ParseError, "Wrong number of arguments " +
@kwilczynski
kwilczynski / array.rb
Created April 6, 2011 20:41
Array#get
class Array
def get(*indices)
indices.empty? ? self : indices.collect { |i| self[i] }
end
end
@kwilczynski
kwilczynski / gist:918742
Created April 14, 2011 01:12
Very simple bash completion for mco from MCollective
_mco()
{
local binary commands current previous
binary=$(which mco 2> /dev/null)
if [[ -x "${binary}" ]] ; then
COMPREPLY=()
current=${COMP_WORDS[COMP_CWORD]}