Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / example.ept
Created September 28, 2013 14:48
EPT: Enhanced Plaintext Table format allows the use of ruby literals. Crazy? Sure! Mark II.
# Encoding: UTF-8
# Column-Separator: |
# Record-Separator: auto
# Name: items
# Headers: true
ID | Name | Price | Inventory
1 | Watch | 2.99$ | 50
2 | Table | 119.99$ | 12
3 | Chair | 49.99$ | 19
@apeiros
apeiros / example.rb
Created September 27, 2013 08:38
Encoding woes
# Same result for the same IBM437 character - whether it's utf-8 or IBM437 encoded at input
JSON.parse(JSON.dump(["\u2561".encode('IBM437')])).first.unpack("U*") # => [9569]
JSON.parse(JSON.dump(["\u2561"])).first.unpack("U*") # => [9569]
@apeiros
apeiros / example01.ept
Created September 27, 2013 06:38
Enhanced Plaintext Table format allows the use of ruby literals. Crazy? Sure!
Encoding: UTF-8
Column-Separator: 7c
Record-Separator: 0a
Name: items
Headers: true
"ID" | "Name" | "Price" | "Inventory"
1 | "Watch" | 2.99 | 50
2 | "Table" | 119.99 | 12
@apeiros
apeiros / layout_helper.rb
Created September 21, 2013 15:25
A rails LayoutHelper
module LayoutHelper
ActionMapping = {
'create' => 'new',
'update' => 'edit',
}
private
def rendered_action
@_rendered_action ||= begin
module Enumerable
def compacting
return enum_for(__method__) unless block_given?
result = []
each do |value|
next if value.nil?
result << value
yield(value)
@apeiros
apeiros / class_callings.rb
Last active December 22, 2015 11:19
class_* convenience variants
class Object
def class_call(*args, &block)
self.class.__send__(caller_locations(1,1).first.label, *args, &block)
end
def class_method(name=caller_locations(1,1).first.label)
self.class.method(name)
end
def class_send(name=caller_locations(1,1).first.label, *args, &block)
self.class.__send__(name, *args, &block)
end
class Object
def class_call(*args, &block)
self.class.__send__(caller_locations(1,1).first.label, *args, &block)
end
end
class Foo
def self.bar
"bar"
end
module A
X = 1
end
module A
class B
def self.x
X
end
end
end
## file a.rb
module A
X = 1
end
## file a/b.rb
module A
class B
def self.x
X
class String
def match_all(regex)
if block_given?
scan(regex) { yield $~ }
else
enum_for(:scan, regex).map { $~ }
end
end
end