Unique Marketing, Guaranteed Results.

Smarter Sequencing in Factory Girl

May 29th, 2009 by Brett Rasmussen

Hal Shearer and I monkey-patched Factory Girl’s sequencing capabilities to allow for pre-defined enumerations to loop through, instead of just infinitely incrementing numbers.

So instead of doing this:

  Factory.sequence :email do |n|
    "person#{n}@example.com"
  end

you could do something like this:

  Factory.sequence(:email, ['angela', 'brett', 'alec']) do |name|
    "#{name}@example.com"
  end

It will start over at the beginning when it’s gone through all of them:

>> Factory.next :email
=> "angela@example.com"
>> Factory.next :email
=> "brett@example.com"
>> Factory.next :email
=> "alec@example.com"
>> Factory.next :email
=> "angela@example.com"
>> Factory.next :email
=> "brett@example.com"

You can also hand it a range (the internal implementation on this is none too efficient, so don’t give it billions at a time):

Factory.sequence(:email, 50..60) do |n|
  "user_#{n}@example.com"
end

>> Factory.next :email
=> "user_50@example.com"
>> Factory.next :email
=> "user_51@example.com"
>> Factory.next :email
=> "user_52@example.com"

The infinitely incrementing counter is still available if you want it:

Factory.sequence(:email, %w[angela brett alec]) do |name,i|
  "#{name}_#{i}@example.com"
end

>> Factory.next :email
=> "angela_0@example.com"
>> Factory.next :email
=> "brett_1@example.com"
>> Factory.next :email
=> "alec_2@example.com"
>> Factory.next :email
=> "angela_3@example.com"
>> Factory.next :email
=> "brett_4@example.com"

This sort of thing is useful when you want two different factories to use the same sequence and have some overlap between the two groups. For example, we need a bunch of email addresses to test on, many of which share the same domain:

Factory.sequence(:name, %w[angela brett alec hal debbie tracey jared]) do |name,i|
  "#{name}_#{i}"
end

Factory.sequence(:domain, %w[something.com example.com mydomain.com]) do |domain|
  domain
end

Factory.define(:email_address) do |f|
  f.address { "#{Factory.next(:name)}@#{Factory.next(:domain)}" }
end

>> 20.times { ea = Factory.build :email_address; puts ea.address }
angela_0@something.com
brett_1@example.com
alec_2@mydomain.com
hal_3@something.com
debbie_4@example.com
tracey_5@mydomain.com
jared_6@something.com
angela_7@example.com
brett_8@mydomain.com
alec_9@something.com
hal_10@example.com
debbie_11@mydomain.com
tracey_12@something.com
jared_13@example.com
angela_14@mydomain.com
brett_15@something.com
alec_16@example.com
hal_17@mydomain.com
debbie_18@something.com
tracey_19@example.com

For our last trick, the reset method returns both the looping index and the infinite counter back to zero:

>> Factory.reset :name
>> Factory.next :name
=> "angela_0"

Here’s the code to make it happen:

class Factory
  def self.sequence(sequence_name, enum = nil, &blk)
    @@sequences ||= {}

    enum = enum.to_a

    @@sequences[sequence_name] = {
      :enum => enum,
      :index => 0,
      :infinite_counter => 0,
      :template  => blk
    }
  end

  def self.next(sequence_name)
    seq = @@sequences[sequence_name]

    retval = case seq[:template].arity
      when 1
        seq[:template].call(seq[:enum][seq[:index]])
      when 2
        seq[:template].call(seq[:enum][seq[:index]], seq[:infinite_counter])
    end

    seq[:index] = (seq[:index]+1 == seq[:enum].size) ? 0 : seq[:index]+1
    seq[:infinite_counter] += 1
    @@sequences[sequence_name] = seq
    retval
  end

  def self.reset(sequence_name)
    @@sequences[sequence_name][:index] = 0
    @@sequences[sequence_name][:infinite_counter] = 0
  end
end

Just put that into some file–perhaps in your rails lib directory–and make sure that file gets required–probably in your rails config/environment.rb. When doing it by hand like this, you’ll want to make sure your library file is loaded after the factory_girl gem is loaded, or you’ll get weirdness like methods you’ve overridden acting in non-overridden ways and the like; config.after_initialize in your environment.rb’s Rails::Initializer block is your friend.

You can also now use the gem BrettRasmussen-factory_girl from gems.github.com. I mean to submit it as a patch back to the original factory_girl, which I’m sure I’ll have time to do Any Day Now.

Chuck’s Ruby Indexer

May 22nd, 2009 by Chuck Wood

I was messing around with ruby a little and decided to write a little indexer that would tell me what the most common words were in my files. It’s really kind of a dumb program, but it was interesting what it turned out when I ran it against my code. The most common word was frequently the word ‘the.’ ‘the’ is not a commonly used variable or function in Ruby. So I looked at my code and realized that it was heavily commented, yielding frequent ‘the’s.

That being said, I’m curious to see what other people find running the indexer against their code. You can get it at http://github.com/woody2shoes/indexer/tree/master. Please comment and let me know what your code looks like.
Read the rest of this entry »

Using IMEEM to Social Network in Business

May 21st, 2009 by Brandon Buttars
IMEEM Profile

I am a big IMEEM fan and I know there are tons of different music social networks out there that can be used, but I just like the way that IMEEM is. I was doing some searching through IMEEM for a good playlist and the first one I ran into was one put together by a user by the name of kiasoul. I went to the playlist and as you would expect, it was a playlist put together by Kia promoting their new Kia Soul vehicle. Genius I thought. I know I may be a little slow because everyone else probably already thought to do this, but I thought I would jump on the same band wagon and let people that hadn’t thought to do it see how I did it.

Read the rest of this entry »

How to do Benchmarking with Ruby

May 21st, 2009 by Alan Carl Mitchell

Benchmarking with Ruby is super easy. There is already a built in class–Benchmark–that will do all of the heavy lifting for you.

If you want to do basic benchmarking, Benchmark.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which one we like best.

require 'benchmark'

n = 5000000

Benchmark.bm do |x|
  x.report("for loop:")   { for i in 1..n; a = "1"; end }
  x.report("times:")      { n.times do   ; a = "1"; end }
  x.report("upto:")       { 1.upto(n) do ; a = "1"; end }
end

This will produce output something like this:

               user     system      total        real
for loop:  0.727000   0.000000   0.727000 (  0.727030)
times:     0.571000   0.000000   0.571000 (  0.571588)
upto:      0.523000   0.000000   0.523000 (  0.522947)

If we use Benchmark.bmbm, then it will do a ‘rehearsal’ run first in order to better equalize the garbage collection environment of the code inside the block so that hopefully we get more realistic timings.

require 'benchmark'

n = 5000000

Benchmark.bmbm do |x|
  x.report("for loop:")   { for i in 1..n; a = "1"; end }
  x.report("times:")      { n.times do   ; a = "1"; end }
  x.report("upto:")       { 1.upto(n) do ; a = "1"; end }
end

This will produce output something like this:

Rehearsal ---------------------------------------------
for loop:   0.756000   0.000000   0.756000 (  0.756298)
times:      0.477000   0.000000   0.477000 (  0.476855)
upto:       0.527000   0.000000   0.527000 (  0.526522)
------------------------------------ total: 1.760000sec

                user     system      total        real
for loop:   0.751000   0.000000   0.751000 (  0.751067)
times:      0.516000   0.000000   0.516000 (  0.515874)
upto:       0.581000   0.000000   0.581000 (  0.581254)

Alternately, you can use the Benchmark.realtime method like this:

puts "for loop: #{Benchmark.realtime {for i in 1..n ; a = "1" ; end}}"

and get something like this:

for loop: 0.682438850402832

??????? ???????? ?????? ?????? ?????? ?? ??????? ??????? ????????????? ?? ????? ???? ?????????? ????????. ??? ???????? ?? ???????, ??????? ???????????? ???????, ??? ???, ??? ????? ???? ??? ?????????, ????????? ???? ? ?????????? ???????????? ???? ?????? ?????????? ?????????? ????????? ?????. ??????? ???????? ?????? ?????? ?????? ?? ????? ???? ?????????? . http://igrat-avtomaty-vulkan.com/igrovye-apparaty/ ??????? ???????? ?????? ????? ?????? ????????? ????????? ?????? ?????????? ?????? ???????? ????, ??? ??????????? ? ???????? ?????????????? ???????? ?? ?????. ??? ???? ? ?????? ?????? ?????? ?? ????? ???? ?????????? ????????. ??? ???? ? ?????? ?????? ?????? ?? ????? ???? ?????????? ????????. ??? ???????? ?? ???????, .

Web Design and SEO Rap

May 8th, 2009 by Brandon Buttars

Here is a sweet rap song about web design coding and SEO.

Google Chrome Commercial

May 8th, 2009 by Brandon Buttars

Here is a sweet Google Chrome advertisement from YouTube. I wonder when Microsoft will come out with one for IE8. Ah oh. Here goes another Mac vs. Windows like fight.

If you don’t need to worry about deadlines and quality of your professor indifferent. Paper writers who work with us, you order with us are competent in all subjects: biology, geography, philosophy, mathematics, sociology, psychology, literature, art, history, and quality of scientific art that won’t leave . Paperell If you order with us are competent in all subjects: biology, geography, philosophy, mathematics, sociology, psychology, literature, art, history, and quality of scientific art that won’t leave your assignment. Our professional paper writing service will create a real piece of your assignment. Our professional paper writing service will create a .

What to do with IE6?

May 5th, 2009 by Mister Mc

bd
I’m really tired of designing a simple, standards-compliant layout that looks fine in every browser but IE6.  Aren’t you?  Almost every web developer has something to say about this topic.  I’ve found some interesting sites recently that suggest action we can take as a design community get IE6 out of our hair.  The suggestions range from mild to extreme; either helping IE6 gracefully exit the stage or die a quick, painful death.  We obviously want to make life easier for ourselves, but we can’t alienate our audience.  I’m most concerned about those who don’t have the ability to upgrade their system because they work for a public institution with ancient machines or proprietary apps that don’t work on other browsers.  For the rest of the world, maybe a little tough love is a good thing.  Here are some of my favorite ideas: Read the rest of this entry »

Ruby newbie iteration musings

May 5th, 2009 by hals

First off I must note how nice it is that semicolons, and several other punctuation items, are optional.

Another nice thing I have been introduced to is the iterator. In most cases it completely replaces the old for loop. 

var.each {|x| ….}     

or

for x in var {……}

seem much cleaner and easier to write than

for (int i=0; i<10; i++) {…; …;}

Extending this makes it handy to work with multiple parameters. Let’s say that you want to pass in a variable number of arguments, that can also be hashes.

One way of working with them in the method would be as follows:

class Ema

def initialize(p1, p2, *p3)  # the ‘*’ will push arguments 3…n into p3 as an array

# then you might access parameter3 with one of the following .each variants:

def showMe

    @p3.each do |s|    #this gives you each element of the array – 

      puts ”  p3: s = #{s}”

        s.each_key do |y|     #each element is a hash, so this gives you the keys

          puts ”     #{y} = #{s[y]}”

        end

        s.each do |y,z|       #this will give you the key, value pairs

          puts ”     #{y} = #{z}”

        end

        s.each do |r|        #and this gives you the key,value in an array

          puts ”     #{r[0]} = #{r[1]}”

        end

    end

end

 a = Ema.new(“one”, “two”, {“apple” => 3}, {“pear” => 1}, {“grape” => 5}, {“kiwi” => 33})

a.showMe

Copyright © 2005-2016 PMA Media Group. All Rights Reserved &nbsp