HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Fabricate with work dates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withdatesworkfabricate

Problem

I am using code like this everywhere. How can I reduce such this code so that my Ruby code looks a lot cleaner?

Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 266,:type => "fast" )
Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 100,:type => "super_fast" )
Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 50,:type => "ludicrous" )
Fabricate(:tl, :when =>Date.yesterday.to_s,:work => 900,:type => "budget" )

Solution

I found that I can use hash({}).each method to dry up the code. It is :

{"fast"=>266, "super_fast" => 100, "ludicrous" => 50, "budget" => 900}.each{ |key, value|
  Fabricate(:tl, :when =>Date.yesterday.to_s,:work => value,:type => key )
}


If there is a better technique, please share it.

Code Snippets

{"fast"=>266, "super_fast" => 100, "ludicrous" => 50, "budget" => 900}.each{ |key, value|
  Fabricate(:tl, :when =>Date.yesterday.to_s,:work => value,:type => key )
}

Context

StackExchange Code Review Q#4210, answer score: 6

Revisions (0)

No revisions yet.