3

Using faker gem with rails to generate some fake data. When I use faker::lorem the output includes dashes in front of the string.

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    7.times do |l|
      line = Line.create!(sentence: Faker::Lorem.sentences(2))
    end
  end
end

Like:

---
- Odit consectetur perspiciatis delectus sunt quo est.
- Tempore excepturi soluta aliquam perferendis.

Any idea why this function returns the Lorem with dashes? Easiest way to strip them out?

Kevin K
  • 2,191
  • 2
  • 28
  • 41
  • I think I figured it out. Lorem.sentences returns an array. When I added .join(" ") on the end, the dashes were gone and it read correctly. The dashes must be what happens when you try to put an array into a string. – Kevin K Aug 31 '12 at 17:55
  • 1
    It's actually the gem generating YAML when converted to a string. – deefour Aug 31 '12 at 19:22

1 Answers1

3

As Kevin and Deefour mention in the comments, Faker::Lorem returns an array.

Since we're asking Ruby to store it in a string, Ruby interprets it per the guidelines for YAML, which is a way of representing data structures in text.

If you're encountering this issue, the simplest fix is to use Ruby's built in array-to-string conversion method .join(x), which takes string x as the delimiter to add.

zrisher
  • 2,215
  • 1
  • 16
  • 12