Skip to content

Instantly share code, notes, and snippets.

:octocat:

Shannon Skipper havenwood

:octocat:
Block or report user

Report or block havenwood

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@havenwood
havenwood / irc_example.rb
Last active Dec 14, 2019
An example for IRC
View irc_example.rb
chars = str
.split
.map(&:chars)
.reduce(&:zip)
.each_slice(2)
.map(&:join)
.map(&:chars)
chars.map { |char| char.map(&HASH) }
@havenwood
havenwood / object.rb
Created Dec 6, 2019
Examples of defining methods and getting and setting instance variables on objects
View object.rb
foo = Object.new
def foo.value
42
end
foo.value
#=> 42
bar = Object.new
@havenwood
havenwood / optional.rb
Created Dec 1, 2019
Playing with chaining #then on a Swift-like Optional
View optional.rb
# frozen_string_literal: true
class Optional
attr_reader :value
def initialize(value)
@value = value
end
def then(&block)
@havenwood
havenwood / async.rb
Created Nov 16, 2019
Example Async equivalent to code naftilos76's code that phaul is helping with on #ruby IRC: https://gist.github.com/phaul/2b2166c913452f010b1513a8165ab287
View async.rb
require 'async'
condition = Async::Condition.new
Async do
task_one = Async do
loop do
puts 'task one before waiting'
condition.wait
end
@havenwood
havenwood / node.rb
Last active Nov 13, 2019
Playing with Enumerator.produce from Ruby 2.7
View node.rb
# frozen_string_literal: true
class Node
attr_reader :value
attr_reader :parent
def initialize(value:, parent: nil)
@value = value
@parent = parent
end
@havenwood
havenwood / erb.rb
Created Nov 12, 2019
Example YAML template for Net on #ruby channel IRC
View erb.rb
require 'erb'
require 'psych'
topic = :users
users = %w[wombat fox poodle]
template = ERB.new <<~END, trim_mode: '-'
---
:<%= topic %>:
<%- users.each do |user| %>
@havenwood
havenwood / make_it_work.rb
Created Nov 7, 2019
An answer to crella133's question in #ruby IRC
View make_it_work.rb
class Foo
def initialize(a, b, c:)
puts 'Foo!'
end
end
class MakeItWork < Foo
def initialize(*args, **kwargs, &block)
parameters = method(__method__).super_method.parameters
View response_enumerator.rb
class ResponseEnumerator < Enumerator
DEFAULT_PER_PAGE = 25
attr_accessor :per_page
def initialize(square, http_verb:, path:, data:,
fields:, parameters: {}, memoize: true, per_page: nil)
@per_page = per_page || DEFAULT_PER_PAGE
@memoized_responses = []
@params = parameters.fetch :params
@havenwood
havenwood / cipher.rb
Created Nov 5, 2019
Ruby Example of AES-256 in CBC Mode for consti on #ruby IRC
View cipher.rb
secret = 'cicada'
key = 'wombat'
cipher = OpenSSL::Cipher::AES256.new :CBC
cipher.encrypt
salt = OpenSSL::Random.random_bytes 16
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1 key, salt, 20000, 32
iv = cipher.random_iv
@havenwood
havenwood / delegate_class.rb
Last active Oct 20, 2019
An example using DelegateClass with core Classes. For the why, see: https://words.steveklabnik.com/beware-subclassing-ruby-core-classes
View delegate_class.rb
class FerretString < String
def to_s
'Successfully overrode ...'
end
end
class WombatString < DelegateClass(String)
def initialize
super('')
end
You can’t perform that action at this time.