Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / ruby_pseudo_globals.c
Last active September 27, 2016 13:03
Globals in ruby which really are native C functions pretending to be globals.
// Ruby 2.3.1
/* eval.c:1614: */ rb_define_virtual_variable("$@", errat_getter, errat_setter);
/* eval.c:1615: */ rb_define_virtual_variable("$!", errinfo_getter, 0);
/* io.c:12280: */ rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
/* load.c:1195: */ rb_define_virtual_variable("$\"", get_loaded_features, 0);
/* load.c:1196: */ rb_define_virtual_variable("$LOADED_FEATURES", get_loaded_features, 0);
/* process.c:7537: */ rb_define_virtual_variable("$?", rb_last_status_get, 0);
/* process.c:7538: */ rb_define_virtual_variable("$$", get_pid, 0);
/* re.c:3653: */ rb_define_virtual_variable("$~", match_getter, match_setter);
/* re.c:3654: */ rb_define_virtual_variable("$&", last_match_getter, 0);
class Temperature
KelvinToCelsius = 273.15
KelvinToFahrenheit = 459.67
FahrenheitFactor = 5.0/9
def self.from_kelvin(kelvin) # could just alias
new(k: kelvin)
end
def self.from_celsius(celsius) # IMO should be just named "celsius"
this is afaik mostly for assignment methods. e.g. `a.foo ||= b` expanding to `a.foo = a.foo || b`
would invoke foo= regardless of whether it's necessary. `a.foo || a.foo = b` would not.
@apeiros
apeiros / assert_unordered_equal.rb
Created January 28, 2014 21:18
Assert two enumerables to be equal, not minding the order
module Test::Unit::Assertions
def assert_unordered_equal(expected, actual, message=nil)
full_message = build_message(message, "<?> expected but was\n<?>.\n", expected, actual)
assert_block(full_message) {
seen = Hash.new(0)
expected.each { |e| seen[e] += 1 }
actual.each { |e| seen[e] -= 1 }
seen.invert.keys == [0]
}
end
@apeiros
apeiros / array_split.rb
Last active January 1, 2016 19:29
Split an Array, similar to String#split
class Array
def split(*separator, include_separator: false)
if separator.empty?
if !block_given?
return enum_for(__method__) if separator.empty? && !block_given?
end
elsif block_given?
raise ArgumentError, "Must either pass a block or a separator, not both"
else
separator = separator.first
@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)