Skip to content

Instantly share code, notes, and snippets.

Avatar

Drew Olson drewolson

View GitHub Profile
View luhn.rb
# example solution, yes i know this is long
def luhn?(number)
digits = ''
number.split('').reverse.each_with_index do |d,i|
if i % 2 == 0
digits += d
else
digits += (d.to_i * 2).to_s
end
end
View masked_number.rb
def mask n;n.gsub(/\s/,'').sub /(\d{6}).*(\d{4})/,"\\1#{'*'*(n.size-10)}\\2";end
View closure_method.js
var colorName = "black";
Ninja.prototype['color'] = function () {
puts(this.name + "'s color is " + colorName);
}
drew.color();
// => Drew's color is black
adam.color();
// => Adam's color is black
View call_dynamically.rb
drew.send(:battle_cry)
# => Drew says zing!!!
View given_time.rb
Given /^I recieved an invitation 2 days ago$/ do
...
end
View given.rb
Given /^I recieved an invitation$/ do
...
end
View cucumber_time.rb
Given /^(.+) (\d+) (seconds?|minutes?|hours?|days?|months?|years?) (ago|from now)$/ do |string, number, time_unit, time_direction|
Timecop.freeze(number.to_i.send(time_unit).send(time_direction.gsub(' ','_'))) do
Given string
end
end
View form.erb
<% form_for [@quiz, @question] do |f| %>
<%= f.label :text, 'Question' %>
<%= f.text_area :text %>
<% 5.times { @question.answers.build } %>
<% f.fields_for :answers do |a| %>
<%= a.label :text, 'Answer:' %>
<%= a.text_field :text %>
<% end %>
View question_factory.rb
Factory.define :question do |q|
q.sequence(:text) { |n| "question #{n}" }
end
Factory.define :question_with_answers, :parent => :question do |q|
correct_answer = Factory.build(:answer)
q.answers do |answers|
(1..4).map { answers.association(:answer) } + [correct_answer]
end
View activetiger.rb
gateway = ActiveTiger::Gateway.new(
:username => "johndoe",
:password => "password"
)
response = gateway.authorize(
:ccnumber => "4111111111111111",
:ccexp => "1010",
:amount => "1.00"
)