Quick tip - Generating long strings for testing

March 9th, 2007 Quick tipsRails

Updated

Next time you need to create long strings to test validations like this:

validates_length_of :details, :maximum => 4000

try this:

# the wrong way
returning(''){|s| 4001.times{|t| s << t.to_s } }

try this (thanks Sergey):

('0'*4001)

Anyone have a better way?

--- --- ---

2 Comments

  1. Comment by Sergey on 03/10/07

    irb(main):005:0> returning(''){|s| 4001.times{|t| s << t.to_s } }.size => 14894

    BUT irb(main):010:0> ("0"*4001).class => String irb(main):008:0> ("0"*4001).size => 4001

  2. Comment by Zack on 03/10/07

    Sergey,

    Nice! I knew ruby had a better way.

Commenting is closed for this article.