2
  def emails
    i = 0
    number = rand(252...4350)

    males = ["tom", "jack", "adam"]
    females = ["elizabeth", "rose", "juliet"]

    surnameMales = ["oak", "yew", "timber"]
    surnameFemales = ["rosewelth", "gates", "jobs"]

    providers = ["gmail.com", "hotmail.com", "yahoo.com"]

    while i <= 100 do
      @addresses << 
    end
  end

What I want to do is pick a random number, name, surname and provider and put them all together + attach a random number at the end of the surname so that it looks, for example, like this: rose.gates643@gmail.com, but kind of got stuck at the point where I have to put the whole thing together. As you can probably tell, I want to create a 100 random emails for testing purposes.

Can you please point me out how I could do what I have intended to do with Ruby?

Xeen
  • 6,955
  • 16
  • 60
  • 111
  • For testing what? You're going to generate email addresses in only one of a multitude of formats. The list of acceptable formats is a lot longer than most people realize, which makes it hard to create a validator that is worth anything. If you just need the one form of "user@host.domain", then sure, your code will work. If you need to test an email gateway or a validator, then no, it won't. – the Tin Man Jan 07 '15 at 18:48
  • @theTinMan I needed to generate some content to show my client how emails would look like in a given part of a website. I wanted to make them as realistic looking as possible, therefor I did it this way. Basically testing wasn't the right way to call it I guess, more like testing "will it work out". ;) Also I didn't use `Faker` due to the need to localize those emails. – Xeen Jan 08 '15 at 06:23

3 Answers3

11

Is your goal to do this as an exercise or is this just something you're trying to get done? If it's the latter, install the faker gem and just use it:

Faker::Internet.email #=> "kirsten.greenholt@corkeryfisher.info"

For 100:

100.times.map { Faker::Internet.email }
Nick Veys
  • 23,458
  • 4
  • 47
  • 64
  • Faker uses a set of domains that can actually be real domains, so make sure you don't use them in case where you might send an email. – Carson Cole Oct 08 '19 at 14:57
3

Use a combination of string interpolation and Array#sample.

"#{females.sample}.#{surnameFemales.sample}#{rand(252...4350)}@#{providers.sample}"
>> "rose.rosewelth3266@gmail.com"
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
1

There's a couple ways to join strings in Ruby. Inline placeholders seems to make the most sense here:

@addressess << "#{ males.sample }.#{ surnameMales.sample }#{ number }@#{ providers.sample }"

In a double-quoted string, #{ expr } is evaluated and subbed inline. "#{ 1 + 1 }" outputs "2", for example.

Also a couple ways you can distinguish male or female — for example, checking if a random number is even or odd:

name = if rand(1..100) % 2 == 0 ? "#{ males.sample }.#{ surnameMales.sample }" : "#{ females.sample }.#{ surnameFemales.sample }"
@addresses << "#{ name }#{ number }@#{ providers.sample }"
Community
  • 1
  • 1
FeifanZ
  • 16,250
  • 7
  • 45
  • 84