tags:

views:

64

answers:

3

I have the following code. I want to use the methods for myserver1 and myserver2 and pass them the addresses the have been iterated through in the send_to_servers method. I can't seem to do that. Please help. Unless I can have these two receive addresses on an altering basis, I won't be able to do what I need to do. Thanks in advance.

class Addresses
  def add
    @addresses = %w([email protected] [email protected] [email protected])
  end

  def myserver1
    puts "Sending email from myserver1 with address #{@address}"
  end

  def myserver2
    puts "Sending email from myserver2 with address #{@address}"
  end

  def servers
    serv = [myserver1, myserver2]
  end
  #  def servers
  #    serv = (1..2).to_a       # Your list of servers goes here
  #  end


  def send_to_servers(servers)
    @addresses.each.with_index do |address, i|
      server = servers[i % servers.length]
      puts "Sending address #{address} to server #{server}"
      @address = address
    end
  end

end


a = Addresses.new
a.add
servers = a.servers
a.send_to_servers(servers)
+1  A: 

It's unclear how you might want to structure it, but here is a concise implementation that I think meets your description.

def myserver1(address)
  # Do something with address
end

def myserver2(address)
  # Do something with address
end

addresses = %w([email protected] [email protected] [email protected])
servers = %w(myserver1 myserver2).cycle
addresses.each do |address|
  send(servers.next, address)
end

Apologies if I'm missing something crucial to your problem with this. Please feel free to comment on what extra functionality is required to help firm up the specification.

fd
I think it's supposed to alternate between servers, and possibly handle a variable number of servers.
AShelly
I tried to stick to the description given in the comments, rather than decipher the nuance of what was intended from looking at the broken code.
fd
I appreciate the code, it would be prefect except for the fact that it doesn't switch between servers when its sends. It send the same message from each server. If you could change it so that it sends the first address from the first server and the second from the next server and so on, that would be great. Thanks for your help!!!
rahrahruby
The reason I want to use method are the each of my servers use different settings and I would have to over write various elements in order to use just a single send block.
rahrahruby
OK, updated so that the methods named in `servers` are called in a round-robin
fd
Dude, you rock!!!!!!! Thanks for your help!!!!!!!!!!!!
rahrahruby
+1  A: 

Your problem is that your server methods do not return anything:

def myserver1
  puts "Sending email from myserver1 with address #{@address}"
end

This method prints the message out and returns nil. puts always returns nil.

Thus, when you do [myserver1, myserver2], it prints out two messages and returns [nil, nil].

Servers are things, they should probably be objects, not methods. Methods are actions that do and/or return something. Try something like this:

class Server
  def initialize(name)
    @name = name
  end

  def send_address(address)
    puts "Sending email from #{@name} with address #{address}"
  end
end

addresses = %w([email protected] [email protected] [email protected])
servers = [Server.new("server one"), Server.new("server two")]
addresses.each_with_index do |address, i|
  server = servers[i % servers.length]
  server.send_address(address)
end
mckeed
can you give me an example of how it should look?
rahrahruby
I added an example. Does this code do what you wanted?
mckeed
So I server.new[Server.new("server one"), Server.new("server two")]would be classes that would contain my server code right?
rahrahruby
I am using the ruby mail class and trying to pass it the "to" the iterated addresses while alternating servers
rahrahruby
I hope that I am explaining what I'm trying to do clearly, my apologizes if it is unclear.
rahrahruby
Yes, you could change the `Server` class to `Mailer` or `MailServer`. Put any code that is needed to assemble and send the message in that class, and create an instance of the class for each server.
mckeed
Looks good, this is certainly where I was going to head with my answer. I was hoping for some confirmation of the desired behaviour first, but it looks like you went straight to the punch. +1
fd
+1  A: 

I believe your code should be refactored to be closer to the real world.

You want to loop through a list of email addresses and send each iteration to myserver2 and myserver2.

This means you need to have the cartesian product of emails and servers and do "send" to that pair.

require 'net/smtp'

emails = %w{[email protected] [email protected]}
servers = %w{server1 server2}
emails.product(servers).each do |address, server|
  Net::SMTP.start(server) do |smtp|
    smtp.send_message 'Body', '[email protected]', [address]
  end
end
Dmytrii Nagirniak
Off topic: you can just put `|address, server|` as the block params. It will unpack automatically.
mckeed
The problem is that I'm using the ruby mail class to send the messages. I want to be able to pass the "to" section of the mail class the iterated email addresses
rahrahruby
@mckeed, thanks for tip - did not know that. I'll update the code.
Dmytrii Nagirniak
thanks for your help
rahrahruby
the mail class takes the following arguments:Mail.deliver do to from subject body end
rahrahruby
Mail.deliver do to from subject body end
rahrahruby
I have updated the answer so it actually sends email.
Dmytrii Nagirniak
This looks good to me. +1
fd
Thanks for your reply!!
rahrahruby