Skip to content

Instantly share code, notes, and snippets.

Avatar
🤖

Michael Kohl citizen428

🤖
View GitHub Profile
View gist:230801a96e9231ad44566083e0c4f6cd
### Keybase proof
I hereby claim:
* I am citizen428 on github.
* I am citizen428 (https://keybase.io/citizen428) on keybase.
* I have a public key whose fingerprint is E183 E826 AAA2 71D9 A25E B8E9 EF16 E5E6 87B2 A218
To claim this, I am signing this object:
@citizen428
citizen428 / with.rb
Created Mar 21, 2012
ActionScript-like "with" for Ruby, result of SO discussion
View with.rb
module Kernel
def with(obj, &block)
obj.instance_eval &block
obj
end
end
@citizen428
citizen428 / array_extension.swift
Last active Jan 26, 2016
Turning Swift into Ruby
View array_extension.swift
import Foundation
extension Array {
func each(task: (Element) -> ()) {
for element in self {
task(element)
}
}
func eachWithIndex(start: Int? = nil, task: (Int, Element) -> ()) {
@citizen428
citizen428 / even_numbers_sequence.swift
Last active Jan 13, 2016
Swift sequences, generators etc
View even_numbers_sequence.swift
class EvenNumbers: CollectionType {
let startIndex : Int
let endIndex : Int
init(start: Int, end: Int) {
self.startIndex = start
self.endIndex = end
}
convenience init() {
@citizen428
citizen428 / userContent.css
Created Jan 16, 2014
Get rid of the "Inland" section of http://derstandard.at. Place contents of this file in `chrome/userContent.css` inside your Firefox profile directory.
View userContent.css
@-moz-document url(http://derstandard.at/) {
div.section.inland { display: none }
}
@citizen428
citizen428 / forth.rb
Last active Sep 29, 2015
NotReallyFORTH
View forth.rb
class Proc
def prim_call(stack)
forth_arity = self.arity - 1
if stack.size < forth_arity
puts "Buffer underrun"
return stack.clear
end
forth_arity > 0 ? self.call(stack, *stack.pop(forth_arity)) : call(stack)
end
end
@citizen428
citizen428 / ruby_github.rb
Created Nov 23, 2011
Create an Atom feed from the new Ruby repos page
View ruby_github.rb
require 'open-uri'
require 'nokogiri'
require 'builder'
html = open("https://github.com/languages/Ruby/created")
doc = Nokogiri::HTML.parse(html)
atom = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
atom.instruct!
atom.feed "xmlns" => "http://www.w3.org/2005/Atom" do
@citizen428
citizen428 / map_reduce.rb
Created Nov 22, 2011
A snippet I used for explaining the map/reduce paradigm
View map_reduce.rb
>> words = "foo bar baz qux foo bar lala"
#=> "foo bar baz qux foo bar lala"
>> # map (user-supplied)
.. mapped = words.split(/ /).map { |w| [w,1] }
#=> [["foo", 1], ["bar", 1], ["baz", 1], ["qux", 1], ["foo", 1], ["bar", 1], ["lala", 1]]
>> # aggregate (built-in)
.. aggregated = mapped.group_by { |w, c| w }
#=> {"foo"=>[["foo", 1], ["foo", 1]], "bar"=>[["bar", 1], ["bar", 1]], "baz"=>[["baz", 1]], "qux"=>[["qux", 1]], "lala"=>[["lala", 1]]}
>> # reduce (user-supplied)
>> aggregated.each_with_object({}) { |(k, v), h| h[k] = v.map(&:last).inject(:+) }
@citizen428
citizen428 / pow-show.rb
Created Nov 22, 2011
A little wrapper around Pow's HTTP requests for status.json and config.jsom
View pow-show.rb
#!/usr/bin/env ruby
require 'json'
unless %w(status config).include?(ARGV[0])
puts "Usage: #{File.basename($0)} status|config"
exit 1
end
jj JSON.parse(`curl -s -H host:pow localhost/#{ARGV[0]}.json`)
@citizen428
citizen428 / zip_with.rb
Created Nov 22, 2011
Haskell's zip_with in Ruby
View zip_with.rb
def zip_with(other, op=nil)
return [] if self.empty? || other.empty?
clipped = self[0..other.length-1]
zipped = clipped.zip(other)
if op
zipped.map { |a, b| a.send(op, b) }
else
zipped.map { |a, b| yield(a,b) }
end
end
You can’t perform that action at this time.