Skip to content

Instantly share code, notes, and snippets.

View mitchty's full-sized avatar

Mitch Tishmack mitchty

  • Saint Paul, MN
View GitHub Profile
@mitchty
mitchty / gist:1263471
Created October 5, 2011 02:22
type, in ruby, only crappily done and not cross platform, THE HORROR
def type(name)
ENV['PATH'].split(File::PATH_SEPARATOR).each do |p|
que = p + File::SEPARATOR + name
return que if File.exists?(que) and File.executable?(que)
end
false
end
type 'foo' # false
type 'openssl' # '/usr/bin/openssl'
@mitchty
mitchty / gist:1359834
Created November 12, 2011 01:01
Dumb monkeypatch for Hash
class Hash
def diff(this)
self.dup.delete_if { |key, val|
this[key] == val
}.merge(this.dup.delete_if { |key, val| self.has_key?(key) } )
end
end
@mitchty
mitchty / probably_the_wrong_thing_todo.rb
Created November 22, 2011 03:41
Dumb oneliner challenge
# So to explain this contrived example a bit. Say you have an array of input that you wanted to match with
# multiple regexes how do you in ~1 line match the lot of things.
#
# So say for a given input of %w/ab abc abcde bcde bc efgh etc/
# You have multiple regexes to match against, in this case its just an array of strings that we build regexes off of.
#
# This is my kitbash first try.
#
# Anyone have any better ideas? I know you could just union them both if === works as your comparison:
# %w/ab abc bc bcde efgh/ & %w/ab bc/
main> [[:a], nil, :b, nil, nil].collect!{|x| Array(x)}
=> [[:a], [], [:b], [], []]
main> RVM::Environment.new.list_strings
=> ["jruby-1.6.5",
"macruby-0.10",
"rbx-head",
"ruby-1.8.6-p420",
"ruby-1.8.7-p352",
"ruby-1.9.2-p290",
"ruby-1.9.3-p0"]
main> RVM::Environment.new.list.rubies
=> ["jruby-1.6.5",
$ ruby -e 'puts "\e(0\e6n\e(B"'
$ ruby -e 'puts "\e(0\e6m\e(B"'
@mitchty
mitchty / lookahead_confusion.pl
Created November 28, 2011 22:42
Confused on positive lookahead in this case
my $var = "anchor1 lkajfdlkj
match
till
the
next
anchor2
more
stuff
anchor3
blah
@mitchty
mitchty / gist:1407710
Created November 30, 2011 02:27
chrome renderer being dumb
Sampling process 28334 for 3 seconds with 1 millisecond of run time between samples
Sampling completed, processing symbols...
Analysis of sampling Google Chrome Helper (pid 28334) every 1 millisecond
Process: Google Chrome Helper [28334]
Path: /Users/Shared/Applications/Google Chrome.app/Contents/Versions/15.0.874.121/Google Chrome Helper.app/Contents/MacOS/Google Chrome Helper
Load Address: 0x6d000
Identifier: com.google.Chrome.helper
Version: 15.0.874.121 (874.121)
Code Type: X86 (Native)
Parent Process: Google Chrome [28232]
@mitchty
mitchty / irb3.rb
Created December 4, 2011 05:36 — forked from peterc/irb3.rb
irb3 - Run an IRB-esque prompt over multiple Ruby implementations at once using RVM
#!/usr/bin/env ruby
# encoding: utf-8
# irb3 - Runs an IRB-esque prompt (but it's NOT really IRB!) over multiple
# versions of Ruby at once (using RVM)
#
# By Peter Cooper, BSD licensed
#
# Main dependency is term-ansicolor for each impl:
# rvm exec gem install term-ansicolor
@mitchty
mitchty / gist:1441323
Created December 7, 2011 03:32
dumb eval loop with output from the eval
$ cat eval_with_comment.rb
b = binding
input = $stdin.read.split(/\n/)
input.each do |line|
line.gsub(/\s+\#.*$/, %q{})
out = eval line, b
puts "#{line} \# #{out.inspect}"
end
$ cat a.rb | ruby eval_with_comment.rb
%w/a b c d/ # ["a", "b", "c", "d"]