The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Even Better Random Times for Rails

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Obie Fernandez

Posts: 608
Nickname: obie
Registered: Aug, 2005

Obie Fernandez is a Technologist for ThoughtWorks
Even Better Random Times for Rails Posted: May 18, 2006 12:13 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Obie Fernandez.
Original Post: Even Better Random Times for Rails
Feed Title: Obie On Rails (Has It Been 9 Years Already?)
Feed URL: http://jroller.com/obie/feed/entries/rss
Feed Description: Obie Fernandez talks about life as a technologist, mostly as ramblings about software development and consulting. Nowadays it's pretty much all about Ruby and Ruby on Rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Obie Fernandez
Latest Posts From Obie On Rails (Has It Been 9 Years Already?)

Advertisement

Here's an even better random date generator, this time Rails style! [FYI: My last entry showed a simple random date function in Ruby.]

First of all, I switched to using a hash parameter to the function, to simulate named parameters just like Rails does. The year_range parameter is still supported, dictating how far back to go for our date, in years. I also added a series parameter, which tells the function to generate a sequence of random dates instead of only one.

class Time

  def self.random(params={})
    years_back = params[:year_range] || 5
    year = (rand * (years_back)).ceil + (Time.now.year - years_back)
    month = (rand * 12).ceil
    day = (rand * 31).ceil
    series = [date = Time.local(year, month, day)]
    if params[:series]
      params[:series].each do |some_time_after|
        series << series.last + (rand * some_time_after).ceil
      end
      return series
    end
    date
  end

end

Why the added complexity and multiple return types? Cause it's a lot more useful that way.

Loading development environment.
>> # a random date
?> Time.random
=> Tue Aug 05 00:00:00 EDT 2003
>>  
?> # birthdays, anyone?
?> 5.times { p Time.random(:year_range=>80) }
Wed Feb 06 00:00:00 EDT 1974
Tue Dec 22 00:00:00 EST 1992
Fri Apr 14 00:00:00 EWT 1944
Thu Jul 01 00:00:00 EDT 1993
Wed Oct 02 00:00:00 EDT 2002
=> 5
>> 
?> # series of dates are useful for account-related info
?> Time.random :series => [20.days, 3.years]
=> [Sat Jan 22 00:00:00 EST 2005,
    Sat Jan 29 12:58:45 EST 2005,
    Fri Sep 08 09:34:58 EDT 2006]
>> 
?> # or maybe to simulate events during an hour?
?> Time.random :series => [1.hour,1.hour,1.hour]
=> [Wed Apr 21 00:00:00 EDT 2004,
    Wed Apr 21 00:45:59 EDT 2004,
    Wed Apr 21 01:02:47 EDT 2004,
    Wed Apr 21 01:31:00 EDT 2004]

Check this out: with the following snippet I accomplish exactly what I was after with regards to populating my database with some realistic sample data. BTW, I'm going to wrap up a polished version of this extension (along with some more sample data goodies that I thought up while I was writing this one) and publish them as a plugin later. Stay tuned for that.

AccountFactMonthly.find(:all).each { |a| a.open_dt, a.first_use_post_dt, a.close_dt = Time.random(:series=>[30.days,5.years]); a.save }

The screenshot shows a portion of my IntelliJ screen, with the SimpleSyntax plugin (for Ruby support) and the SQL plugin (for adhoc queries and schema inspection)

Read: Even Better Random Times for Rails

Topic: Stick it in Your ~/.irbrc: MethodFinder Previous Topic   Next Topic Topic: Sun promete que liberará código fuente de Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use