Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / ruby_string_scrubbed_utf_8.rb
Last active December 18, 2015 07:39
Clean a string from invalid byte sequences.
class String
# Defaults for String#scrubbed_utf8's options argument
ScrubbedUtf8Defaults = {invalid: :replace, undef: :replace}
# Similar to what String#encode does, the options argument is also the same,
# but it defaults to :replace for both :invalid and :undef options.
#
# :invalid:
# If the value is :replace, #encode replaces invalid byte sequences in str
# with the replacement character. The default is :replace.
@apeiros
apeiros / part_of_irbrc.rb
Created May 7, 2013 17:25
Enter passwords in pry/irb without echo
require 'io/console' # requires ruby 1.9.3
module PasswordString
def inspect; "[PASSWORD]"; end
def to_s; "[PASSWORD]"; end
def dup; obj=super;obj.extend PasswordString; obj; end
def clone; obj=super;obj.extend PasswordString; obj; end
end
def password
require 'yaml'
class Configuration
def self.load_yaml(file)
new(YAML.load_file(file))
end
def initialize(config, schema=nil)
config.each do |key, value|
value = Configuration.new(value) if value.is_a?(Hash)
def Object.create(attrs)
obj = new
obj.singleton_class.send(:attr_accessor, *attrs.keys)
attrs.each do |name,value| obj.instance_variable_set(name.to_s.sub(/\A@?/, '@'), value) end
obj
end
o = Object.create(foo: 12, bar: "hi") # => #<Object:0x007fc53a0f7bc8 @bar="hi", @foo=12>
o.bar # => "hi"
# encoding: utf-8
require 'sorting/ascending'
require 'sorting/descending'
module Sorting
@apeiros
apeiros / ri Object#<=>
Last active December 14, 2015 02:49
Better docs for Object#<=>
= Object#<=>
(from ruby site)
------------------------------------------------------------------------------
obj <=> other -> 0 or nil
------------------------------------------------------------------------------
Returns 0 if obj === other, otherwise nil.
@apeiros
apeiros / gist:5016950
Created February 22, 2013 22:12
When trying to tweet the following, I get "There was an error posting your message. forbidden". What the heck? Also, what about a more useful message than just "forbidden"?
Released gem 'sorting'. It provides methods to sort_by asc & desc, and lazily evaluate sort values: https://rubygems.org/gems/sorting feedback welcome!
@apeiros
apeiros / git_revision_entries.rb
Created February 7, 2013 20:34
Efficiently read arbitrary directories of an arbitrary git revision from a git repo into memory, without affecting the working directory.
# BEWARE! This code is largely untested and not ready for production!
Result = Struct.new(:entries, :raw_error, :status)
class Entry
HeaderUnpackFormat = "Z100A8A8A8A12A12A8aZ100A6A2Z32Z32A8A8Z155".freeze
EmptyString = "".freeze
OctalFields = [1,2,3,4,5,6,10,13,14]
def self.from_stream(io)
header = io.read(512)
@apeiros
apeiros / gist:4732913
Created February 7, 2013 18:12
Copy a directory from the repository in a specific revision outside the repository
ref=`git symbolic-ref --short HEAD` && \
git checkout SOME_REVISION && \
cp -r some/dir /to/dir/outside/of/repo && \
git checkout $ref
@apeiros
apeiros / inspector.rb
Created November 17, 2012 15:23
Inspect objects which redefined to_s
# encoding: utf-8
# Sadly, in ruby <2.0 Object#inspect uses to_s, and will use a redefined to_s. Not even
# Object.instance_method(:inspect).call(obj) gets you around that.
# This module provides something akin to Object#inspect for arbitrary objects.
#
# @example Generic object inspection
# puts Inspector.inspect_object(some_obj)
#
# @example Including it into a class