Skip to content

Instantly share code, notes, and snippets.

@numinit
Created September 10, 2012 06:22
Show Gist options
  • Save numinit/3689213 to your computer and use it in GitHub Desktop.
Save numinit/3689213 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# `ruby dm.rb 1d8`, `ruby dm.rb 3d8+5`, `ruby dm.rb 100d10+3`, etc...
# down to one line now ;)
['Rolling... '.tap{%w(colorize net/http).map &method(:require)}.blue, ((res=->(d, rnd){d.count > 1 ? ((quota = (get = ->(uri={}) {Net::HTTP.start((u = URI::HTTP.build(uri.merge(host: 'www.random.org'))).hostname, u.port) {|h| h.request(Net::HTTP::Get.new(u.request_uri, {'User-Agent' => '<user agent here>'}))} rescue nil}).call(path: '/quota/', query: URI.encode_www_form(format: :plain))).is_a?(Net::HTTPSuccess) && quota.body.strip.to_i > 0 ? ((result = get.call(path: '/integers/', query: URI.encode_www_form(format: :plain, rnd: :new, num: d.first, col: d.first, min: 1, max: d[1], base: 10))).is_a?(Net::HTTPSuccess) ? result.body.split.collect(&:to_i) : rnd.call(d[0], d[1])) : rnd.call(d[0], d[1])) : 'invalid or no dice provided'}.call(((a=ARGV.first.to_s.split(/[d+]/).collect(&:to_i).reject{|d| d <= 0}).length == 2 ? a.push(0) : a), ->(d, n){Array.new(d) {rand(1..n)}})).is_a?(Array) ? "#{res.collect{|r| r.to_s.red}.join(', '.uncolorize)}: #{(res.inject(&:+) + a.last).to_s.green}" : res) + "\n"].each(&:display)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment