Skip to content

Instantly share code, notes, and snippets.

View patshaughnessy's full-sized avatar

Pat Shaughnessy patshaughnessy

View GitHub Profile
after :: String -> [String] -> [String]
after _ [] = []
after target (x:xs)
| isInfixOf target x = [x] ++ xs
| otherwise = after target xs
main = do
contents <- readFile "the-lake-isle-of-innisfree.txt"
putStr . unlines . after "glimmer" . lines $ contents
Wow Justin, I really loved this! My only criticism is that it’s too bad you
used CoffeeScript instead of Ruby :)
I especially loved the top-down approach you used in your example. I’ve started
to use this all the time myself. I actually wrote about this recently:
http://patshaughnessy.net/2014/2/10/use-an-ask-dont-tell-policy-with-ruby
For me, the biggest benefit of the top-down (“Ask, Don’t Tell” - apologies to
Dave Thomas) style is how it encourages a functional coding style. There are
just so many benefits of doing this. Since I’ve been doing this at a micro
@patshaughnessy
patshaughnessy / gist:a6b2c53a56d288f8dbf6
Last active August 29, 2015 14:01
Teaching Kids Programming Discussion
This weekend Ashley Williams (@ag_dubs), Jeff Casimir (@j3) and I had a brief
discussion on Twitter about teaching programming to kids. We thought this would
be a topic that many other people might be interested in too, so I setup this
page in case anyone else wants to join the discussion.
We're not sure where this will lead - as a next step we could setup a Google
group, Discourse site, Basecamp or some other forum for a longer discussion.
Here's the original Twitter conversation:
@patshaughnessy
patshaughnessy / gist:81b40188214259678541
Last active August 29, 2015 14:21
A quick look at when ActiveRecord instantiates Ruby objects
With regard to this twitter conversation:
https://twitter.com/PericlesTheo/status/601745115074420736
Hi Pericles,
This morning I took a quick look at how ActiveRecord converts the query result
back into Ruby objects.
Here’s where it happens:
def avatar_geometry(style = :original)
@geometry ||= {}
begin
path = avatar.path(style)
unless path
temp_file = Tempfile.open("#{avatar_file_name}_#{style}")
temp_file << avatar.file_contents(style)
path = temp_file.path
end
@geometry[style] ||= Paperclip::Geometry.from_file path
@patshaughnessy
patshaughnessy / hostess.rb
Created January 29, 2012 19:17
Part of the Hostess class from RubyGems.org
class Hostess < Sinatra::Base
... etc ...
def serve_via_s3
serve do
redirect "http://#{$rubygems_config[:s3_domain]}#{request.path_info}"
end
end
@patshaughnessy
patshaughnessy / pusher.rb
Created January 29, 2012 19:19
Part of the Pusher class from RubyGems.org
class Pusher
attr_reader :user, :spec, :message, :code, :rubygem, :body, :version, :version_id
def initialize(user, body, host_with_port=nil)
@user = user
@body = StringIO.new(body.read)
@indexer = Indexer.new
@host_with_port = host_with_port
end
def cripple_rubygems(specs)
reverse_rubygems_kernel_mixin
executables = specs.map { |s| s.executables }.flatten
Gem.source_index # ensure RubyGems is fully loaded
::Kernel.send(:define_method, :gem) do |dep, *reqs|
...etc...
@patshaughnessy
patshaughnessy / gist:1979298
Created March 5, 2012 17:01
Ruby parse string backtrace
(gdb) bt
#0 str_new (klass=4303856320, ptr=0x100680500 "The Ruby core team is playing a joke on us!", len=43) at string.c:389
#1 0x00000001001179f8 in rb_str_new [inlined] () at /Users/pat/.rvm/src/ruby-1.9.3-preview1/string.c:416
#2 0x00000001001179f8 in rb_enc_str_new (ptr=<value temporarily unavailable, due to optimizations>, len=<value temporarily unavailable, due to optimizations>, enc=0x1004027e0) at string.c:430
#3 0x000000010009f725 in parser_str_new (p=<value temporarily unavailable, due to optimizations>, n=<value temporarily unavailable, due to optimizations>, enc=0x1004027e0, func=2, enc0=0x1004027e0) at parse.y:5401
#4 0x00000001000aa677 in parser_parse_string (parser=0x10060e000, quote=0x1010793e0) at parse.y:6033
#5 0x00000001000ab285 in parser_yylex (parser=0x10060e000) at parse.y:6645
#6 0x00000001000b4999 in ruby_yyparse (parser=<value temporarily unavailable, due to optimizations>) at parse.y:7922
#7 0x00000001000c24bf in yycompile0 (arg=<value temporarily unavailable, due to optimi
I go to the home page
And I follow "sign up"
And I fill in username with "Matt"
And I fill in password with…