This file contains 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 '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 |
This file contains 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 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 |
This file contains 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
>> 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(:+) } |
This file contains 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/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`) |
This file contains 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 'date' | |
require 'rexml/document' | |
include REXML | |
doc = Document.new(File.new(ARGV[0])) | |
format = ARGV[1] || "apache" | |
formats = { | |
:apache => "RewriteRule ^%s$ /%s [R=301,L]", |
This file contains 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
(ns step1) | |
(defn fact | |
[n] | |
(if (< n 2) 1 (* n (fact (dec n))))) | |
;; (fact 5) => 120 |
This file contains 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
(use '(incanter core charts io)) |
This file contains 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
;;; Ruby/Rails development | |
;; RVM support | |
;; Ruby Version Manager | |
(require 'rvm) | |
(rvm-use-default) | |
;; automatically add 'end' after class, module, def etc. | |
;; automatically pair braces, single and double quotes | |
(require 'ruby-electric) |