AnApprentice
AnApprentice

Reputation: 111000

Rails A method with 2 returns - how to get them?

parse_emails('long list')


def parse_emails(emails)
  .... stuff
  return valid_emails, invalid_emails
end

I see that there are two arrays but how do I get them? I tried:

mylist = parse_emails('long list')
mylist[valid_emails]

but that error'd. ideas? thxs

Upvotes: 1

Views: 772

Answers (3)

laura
laura

Reputation: 2961

You could pass the two arrays into the method, then populate them in there:

invalid_emails = []
valid_emails = []
parse_emails('long list', valid_emails, invalid_emails)


def parse_emails(emails, valid, invalid)
  .... stuff
  while ...
    if is_valid?
      valid << current_email
    else 
      invalid << current_email
    end
  end
end

Upvotes: 0

9000
9000

Reputation: 40894

valid_emails, invalid_emails = parse_emails(whatever)

Please take time to read some basic intro into Ruby syntax ;)

Upvotes: 7

Mahesh Velaga
Mahesh Velaga

Reputation: 21971

Construct an class with two lists in it, one for valid emails and one for invalid emails and populate the object of this class in parse_emails function and return it.

Then, in the outside, you can access those two lists as properties of the return value.

Upvotes: 1

Related Questions