Avatar
I like futurama, websites, and code.

dharbin:

TYRANNOSAURUS BATS! Now available as an 8” x 10” color indigo-process print. Perfect for that hard-to-shop-for superhero with a taste for expensive robot dinosaur sidekicks!

(via sirmitchell)

Dubstep Lyrics: Skrillex - First of the Year (Equinox) 

dubsteplyrics:

http://www.youtube.com/watch?v=TYYyMu3pzL4


- Eclectic Introduction -


Aeg eh euf, gyaaaa uh ah uh, gye gye gye gyeeeeuw.

Aeg eh euf, gyaaaa uh ah uh, gyuh nyi nyi nyi nyiiii nyao ni ni ni.

Aeg eh euf, gyah, ah, yef, yaaiooiiiiinaaiiooneueuaodeh.

Aeg eh euf, gyaaaa uh ah uh, gye…

Deterministic Password Generation

I wrote a quick script to generate passwords given a master password. This way you only have to remember a single password, and with that password you can generate all of your other passwords. Instructions on how to use this script can be found here: https://github.com/akdarrah/scripts

Here is the script:

require 'rubygems'
require 'digest'
require 'highline/import'
require "backports"

# if char is a letter, capitalize it. if it is a number, replace with a symbol
def alter_char(char)
  symbol_set =  %w(@ # % ^ & * +  ?)
  char.to_i.zero? ? char.to_s.upcase : symbol_set[rand(char.to_s.ord) % symbol_set.size]
end

# replaces about half of the characters with a capital letter or symbol
def format(raw)
  return formatted = "".tap do |formatted|
    raw.each_char do |char|
      formatted_char = (rand(raw.size) % 2).zero? ? alter_char(char) : char.to_s
      formatted.concat formatted_char
    end
  end
end

identifier = ARGV.join("")
secret_key = ask("Password: "){|q| q.echo = false}
srand(secret_key.each_char.map{|c| c.to_s.ord}.sum.to_i)
password = format Digest::MD5.hexdigest(identifier + secret_key)
%x[printf '%s' '#{password}' | pbcopy]
say "Generated password copied to clipboard."
Basically, it takes your input and asks for your master password. With these two parameters, it generates your password and copies it to your clipboard. I wanted to write about this because the generation needs to be deterministic, meaning that it will generate the same password for the same input every time. Even though I rely on random numbers to format the final generated password, the random numbers that are generated are the same every time. This works because I seed the random numbers based on the given master password before generating the numbers. Here’s an example of how this works:

Generating random numbers without seeding results in different random numbers:
adamdarrah@onebert~/note $ rails console
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
Loading development environment (Rails 3.0.1)
ree-1.8.7-2011.03 :001 > rand
 => 0.279273463333317 
ree-1.8.7-2011.03 :002 > rand
 => 0.216048229607135 
ree-1.8.7-2011.03 :003 > rand
 => 0.300257519246147 
ree-1.8.7-2011.03 :004 > exit
adamdarrah@onebert~/note $ rails console
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
Loading development environment (Rails 3.0.1)
ree-1.8.7-2011.03 :001 > rand
 => 0.426064545572545 
ree-1.8.7-2011.03 :002 > rand
 => 0.756376225221752 
ree-1.8.7-2011.03 :003 > rand
 => 0.259909140308626
If you seed before generating your numbers, you get the same numbers:
adamdarrah@onebert~/note $ rails console
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
Loading development environment (Rails 3.0.1)
ree-1.8.7-2011.03 :001 > srand(999)
 => 28212304341349319917058999759470316901167389175548446547334060628152746438732 
ree-1.8.7-2011.03 :002 > rand
 => 0.803428040079688 
ree-1.8.7-2011.03 :003 > rand
 => 0.527522295682645 
ree-1.8.7-2011.03 :004 > rand
 => 0.119111465028212 
ree-1.8.7-2011.03 :005 > exit
adamdarrah@onebert~/note $ rails console
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
Loading development environment (Rails 3.0.1)
ree-1.8.7-2011.03 :001 > srand(999)
 => 5153426315139723512360796695679417744676510232904009578147330885438135587813 
ree-1.8.7-2011.03 :002 > rand
 => 0.803428040079688 
ree-1.8.7-2011.03 :003 > rand
 => 0.527522295682645 
ree-1.8.7-2011.03 :004 > rand
 => 0.119111465028212 
So even though I’m using random numbers to format the final password, which you may expect to be different, the generated random numbers end up being the same every time. This deterministic approach also plays a huge role in RTS game engines. You can read about that here: http://altdevblogaday.com/2011/07/09/synchronous-rts-engines-and-a-tale-of-desyncs/

Testing a before_save Callback with Rspec

I couldn’t find any online resources on how to test a rails model before_save callback method. Here is a way you can test your before_save callback without actually saving the model (credit goes to @dcsesq).
# Model code

before_save :before_save_callback_method

def before_save_callback_method
  self.name = "derp"
end
# Model Spec code

describe '#before_save_callback_method' do
  
  # test that the callback is called...
  it "should call before_save_callback_method before a save" do
    @model = stub_model(Model)
    @model.stub!(:valid?).and_return(true)
    @model.should_receive(:before_save_callback_method).and_return(false)
    @model.save
  end
  
  # test your method works...
  it "should set name of model to 'derp'" do
    @model = stub_model(Model, :name => "")
    @model.before_save_callback_method
    @model.name.should == "derp"
  end
  
end
The spec works because of this line:
@model.should_receive(:before_save_callback_method).and_return(false)

After the model receives the before_save_callback_method method, it returns false and doesn’t go through with the save.

EDIT: It turns out you can access the list of callbacks a rails model has defined. So instead of testing the behavior of rails, which is already tested, we can just make sure that our callback is in the list of defined callbacks (Thanks to @andyfleener for pointing this out).

So this test:
# test that the callback is called...
it "should call before_save_callback_method before a save" do
  @model = stub_model(Model)
  @model.stub!(:valid?).and_return(true)
  @model.should_receive(:before_save_callback_method).and_return(false)
  @model.save
end
Could be replaced with this test:
# test that the callback is called...
it "should have before_save_callback_method defined as a before_save callback" do
  Model._save_callbacks.select { |cb| cb.kind.eql?(:before) }.map(&:raw_filter).include?(:before_save_callback_method).should == true
end
A big advantage with this approach is that it will work with any callback type.

moustair:

Bruce Willis Yippee Ki Yay Moustair

sirmitchell:

……………………….NO!

sirmitchell:

……………………….NO!

Saved from my posterous, incase you want to use this with fluid!

Saved from my posterous, incase you want to use this with fluid!

Reddit IAMA Hide Script

Here’s a quick script I wrote to hide comment threads on reddit.com in which the original author of the submission has not participated in. It will work for any subreddit, but I think this script will be most useful in the IAMA subreddit since I personally only read comments if I see that there is a response by the IAMA submitter. To use the script, simply paste the following javascript code into the url box of your browser and hit enter.

javascript:$(".thing.comment").each(function(){if($(this).parents(".child").length == 0){if($(this).find(".submitter").length == 0){$(this).hide();}}}); void(0);
EDIT: I’m not sure what’s going on with the copy link above not working. Try this:

https://gist.github.com/782169

EDIT: Imported from my posterous!

Next page Something went wrong, try loading again? Loading more posts