Skip to content

Instantly share code, notes, and snippets.

View gsamokovarov's full-sized avatar
👾
...

Genadi Samokovarov gsamokovarov

👾
...
View GitHub Profile
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'gsamokovarov/rails', branch: 'fix-null-resolver'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
GEMFILE
system 'bundle'
@gsamokovarov
gsamokovarov / gist:f2dc3b26857b134c8b21
Created August 23, 2014 17:06
Segfault on Ruby.2.1.1 under Ubuntu precise
die_ruby.rb:10: [BUG] Segmentation fault at 0x007fff9cd5cfe8
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0002 p:0015 s:23794 E:001928 EVAL die_ruby.rb:10 [FINISH]
c:0001 p:0000 s:0002 E:0016d8 TOP [FINISH]
die_ruby.rb:10:in `<main>'
-- C level backtrace information -------------------------------------------
class Exception
def set_backtrace_with_extension
set_backtrace_without_extension(*args)
end
alias set_backtrace_without_extension set_backtrace
alias set_backtrace set_backtrace_with_extension
end
raise
@gsamokovarov
gsamokovarov / console.rb
Created August 7, 2013 18:54
PTY.spawn attaches a psuedo-terminal device to the standard input.
require 'pty'
PTY.spawn 'irb' do |output, input, pid|
loop do sleep 1
input.puts 'puts :Wootz!'
puts output.gets
end
end
__END__
# If you truncate a StrinIO and don't rewind it, the next write will have
# leading zeroes.
require 'stringio'
s = StringIO.new
s.write 'foo'
p s.string # => "foo"
s.truncate 0
# s.rewind
require 'irb/xmp'
xmp = XMP.new(binding)
xmp.puts "foo = 42"
xmp.puts "foo"
@gsamokovarov
gsamokovarov / power.py
Created June 13, 2013 15:09
Logarithmic integral exponent power implementation in Python
def power_linear(number, exponent):
return sum([number * number for _ in range(exponent)])
def power_log(number, exponent):
if exponent == 0:
return 1
half = power_log(number, exponent / 2)
return half * half if exponent % 2 == 0 else number * half * half
@gsamokovarov
gsamokovarov / permute.py
Created June 13, 2013 15:09
Iterative permutation implementation in Python.
def permute(sequence, index=0):
length = len(sequence)
if index > length:
raise StopIteration
if index == length:
yield sequence
else:
for i in range(index, length):
@gsamokovarov
gsamokovarov / underscore-invalid-date.js
Created January 28, 2013 13:16
Simple check for invalid JavaScript dates.
_.mixin({
// Invalid date predicate. The invalid dates are still `Date` instances, however their time is `NaN`.
isInvalidDate: function(obj) {
return _.isDate(obj) && _.isNaN(obj.getTime());
}
});
@gsamokovarov
gsamokovarov / carmine-image-formatter-example.rb
Created August 12, 2012 15:38
Carmine Example - Image Formatter
require 'carmine'
# Opinionated client.
carmine = Carmine.new(
# Default to JavaScript lexing, if that's what the examples
# would be written in.
:lexer => :javascript,
# An image, so we can embed it into presentations.
:formatter => :png,
:options => {