Skip to content

Instantly share code, notes, and snippets.

View rantler's full-sized avatar

Randy Antler rantler

  • Computer Diversions
  • Seattle, WA
  • 18:25 (UTC -07:00)
View GitHub Profile
class Foo
def meth(a:, b:, c:)
method(__method__).parameters.map(&:last).each do |var|
instance_variable_set("@#{var}", binding.local_variable_get(var))
end
end
def inspect
puts "@a = #{@a}"
puts "@b = #{@b}"
puts "@c = #{@c}"
require 'base64'
class ValidateCsrfTokens
class << self
def validate(form_token:, cookie_token:)
decoded_form_token = Base64::decode64(form_token)
decoded_cookie_token = Base64::decode64(cookie_token)
form_token_length = decoded_form_token.bytes.length
cookie_token_length = decoded_cookie_token.bytes.length
@rantler
rantler / sketch.rb
Last active August 6, 2018 18:27
Raise custom exception on request error
class ApprovalError < StandardError
def initialize(response)
@response = response
@request = response.request
end
def inspect
"uri: #{@request.uri}, response_body: #{@response.body}"
end
end
@rantler
rantler / fast_fib.rb
Created June 1, 2017 21:30
Example of fast fibonacci algorithm in Ruby
# Cribbed from https://stackoverflow.com/questions/24438655/ruby-fibonacci-algorithm
def fib_memo(n, memo)
memo[n] ||= fib_memo(n-1, memo) + fib_memo(n-2, memo)
end
def fib(n)
raise "fib not defined for negative numbers" if n < 0
fib_memo(n, [0, 1])
end
@rantler
rantler / gist:7742981
Last active December 29, 2015 23:29
How to *actually* replace invalid UTF-8 characters in string. :-)
a = "abc\xffdef"
a.force_encoding('UTF-8') # => "abc\xFFdef"
a.encoding.name # => "UTF-8"
# ok
a.valid_encoding? # => false
# wtf?
@rantler
rantler / gist:7403845
Last active December 27, 2015 23:09
Simple solution to time diff in clock face degrees. With tests.
class DegreeClock
HOURS_PER_DAY = 24
MINUTES_PER_HOUR = 60
DEGREES_PER_HOUR = 360
DEGREES_PER_DAY = DEGREES_PER_HOUR * HOURS_PER_DAY
DEGREES_PER_MINUTE = DEGREES_PER_HOUR / MINUTES_PER_HOUR
HOURS_PER_HALF_DAY = HOURS_PER_DAY / 2
class Time
# https://rpm.newrelic.com/account/131856/agent/3256655/metrics/425412176
# https://rpm.newrelic.com/accounts/131856/plugins?type=6106
# http://note.io/1bjT5FW
[<2013-11-08 23:00:22, 2013-11-08 23:01:22> metric: count:1 total:7.0,
<2013-11-08 23:01:21, 2013-11-08 23:02:21> metric: count:1 total:7.0,
<2013-11-08 23:02:21, 2013-11-08 23:03:21> metric: count:1 total:7.0,
<2013-11-08 23:03:22, 2013-11-08 23:04:22> metric: count:1 total:7.0,
<2013-11-08 23:04:22, 2013-11-08 23:05:22> metric: count:1 total:7.0,
<2013-11-08 23:05:23, 2013-11-08 23:06:23> metric: count:1 total:7.0,
<2013-11-08 23:06:23, 2013-11-08 23:07:23> metric: count:1 total:7.0,
@account = Account.find(131856)
@account.set_shard_connection
@agent = Agent.find(3256655)
@metric = Metric.find(425412176)
time_window = TimeWindow.new((Time.now.utc - 1.day).to_i, Time.now.utc.to_i)
@timeslice_data = Timeslice.find_and_aggregate(@agent, time_window, @metric, :aggregate_time => false, :raw_data => true)
class PluginRater
def self.rate_plugin_for_user!(rating, plugin, user)
plugin.update_rating_for_user(rating, user)
Event::Publish('user_rated_a_plugin', rating, plugin, user)
Auditor::AuditRatingChange(rating, plugin, user)
end
end
class Controller
def rate
@rantler
rantler / gist:7309428
Last active December 27, 2015 10:09
Software Developer Interview Questions

(See also https://gist.github.com/ryansobol/5252653)

Originally published November, 2013

Javascript

How would you toggle the visibility of several controls on the page?

$$('#element_one, #element_two').invoke('toggle');