Skip to content

Instantly share code, notes, and snippets.

@alexbartlow
alexbartlow / gist:1084056
Created July 15, 2011 04:22
Escape Chars
{
:close_dquote => encode_fallback('”', encoding, '"'),
:close_squote => encode_fallback('’', encoding, '\''),
:copyright => encode_fallback('©', encoding, '(c)'),
:ellipsis => encode_fallback('…', encoding, '...'),
:em_dash => encode_fallback('—', encoding, '---'),
:en_dash => encode_fallback('–', encoding, '--'),
:open_dquote => encode_fallback('“', encoding, '"'),
:open_squote => encode_fallback('‘', encoding, '\''),
:trademark => encode_fallback('®', encoding, '(r)'),
@alexbartlow
alexbartlow / gist:1293051
Created October 17, 2011 16:46
Mixin based coffeescript classes
class Composable
@include = (modules...) ->
@composable_modules ||= []
for module in modules
@composable_modules.push module
apply_mixins: ->
@__proto__.constructor.composable_modules ||= []
for module in @__proto__.constructor.composable_modules
for key, value of module.prototype
@alexbartlow
alexbartlow / gist:1360954
Created November 12, 2011 19:00
Ideal Completion Syntax
module UserHelper
class UserCompletion < Completely
step "user_completion.fill_out_profile", :link => user_profile_path do
user_profile.has_all_data?
end
list_elt 'ul.user_completion'
item_elt 'li.user_step'
item_completed_class 'completed'
@alexbartlow
alexbartlow / gist:5164760
Created March 14, 2013 20:08
Define_method with a block
def self.define_user_field(field_name, &block)
block ||= lambda{|x| x}
define_method("user_#{field_name}"} do |f|
if user_signed_in?
block.call(current_user.send(field_name))
else
f.text_field field_name
end
end
end
def method_with_defaults(options={})
defaults = {
foo: 1,
bar: 2,
baz: 'some_string'
}
arguments = defaults.merge(options)
puts arguments.inspect
@alexbartlow
alexbartlow / gist:5667286
Created May 29, 2013 01:03
Justin's Ruby Learning
puts "Enter your name"
name = gets
length = name.length - 1
print "Your name is "
print length
print " characters long."
# ok, so remember what we talked about with .chomp - whenever you use gets, you'll get the extra newline character on the end,
# (\n) - so chomp will remove that. That's why you have to subtract 1 to get the right length, so we can revise the code to the following
class Message < ActiveRecord::Base
scope :last_two_weeks, -> {
where(:created_at => (2.weeks.ago..Time.now))
}
scope :for_month, (date) -> {
where(:created_at => (date.beginning_of_month..date.end_of_month))
}
def self.sum_word_count
#!/usr/bin/env ruby
specs_to_run = `git status --porcelain | grep "spec"`.each_line.map{|line| line.split(/\s+/).last}
specs_to_run.reject!{|spec| spec =~ %r{^spec/support} }
puts "running #{specs_to_run.join(',')}"
`git add spec && git stash save --keep-index`
account-plugin-aim install
account-plugin-facebook install
account-plugin-flickr install
account-plugin-google install
account-plugin-jabber install
account-plugin-salut install
account-plugin-twitter install
account-plugin-windows-live install
account-plugin-yahoo install
accountsservice install
The signing implementation
The SignImp class isn’t much different from all the examples we’ve written before. When I write
Servlets that involve the use of iText, I always start by writing a short standalone application with a
main() method. This way I can test the code before integrating it into a web application.
In code sample 4.11, I’ve removed the main() method for brevity.
Code sample 4.11: the SignImp class
public class SignImp {
private PrivateKey pk;
private Certificate[] chain;
public SignImp(PrivateKey pk, Certificate[] chain) {