namespace question.

0 views
Skip to first unread message

Tim Uckun

unread,
May 18, 2008, 6:47:57 PM5/18/08
to wellr...@googlegroups.com
This is not a rails question per se but it's a ruby question.

I am having some namespace issues. For example I have the following.


require 'net/smtp'

module Tim
module Net
def Net.send_email (to, subject, msg, from =
'timu...@gmail.com', smtpserver = Tim::Config.mail_server)
tostr = if to.is_a?(Array)
to.join(",")
elsif to.is_a?(String)
to
else
""
end

msg="To: #{tostr}\nFrom: #{from}\nSubject: #{subject}\n\n#{msg}"

Net::SMTP.start(smtpserver) do |smtp|
smtp.send_message( msg, from, to )
end
end #send email
end #module net
end #module Tim


This causes an error because the code thinks Net::SMTP should be in my
module. How do I refer to the SMTP in the Net library explicitly?

I have similar problems when I try to define Tim::Logger or something similar.

Thanks.

Michael Koziarski

unread,
May 18, 2008, 7:02:17 PM5/18/08
to WellR...@googlegroups.com
> This causes an error because the code thinks Net::SMTP should be in my
> module. How do I refer to the SMTP in the Net library explicitly?

::Net::SMTP is what you're after.


> I have similar problems when I try to define Tim::Logger or something similar.
>
> Thanks.
>
> >
>

--
Cheers

Koz

Tim Uckun

unread,
May 18, 2008, 7:05:27 PM5/18/08
to WellR...@googlegroups.com
> ::Net::SMTP is what you're after.

I know. If you look at my code you'll see that I am trying to use
NET::SMTP from inside of my module Tim::Net.

Michael Koziarski

unread,
May 18, 2008, 7:06:48 PM5/18/08
to WellR...@googlegroups.com

You asked how to specify a Net module that's at the root not insude
Tim. You prefix it with those two colons :)

::Net::SMTP


--
Cheers

Koz

Tim Uckun

unread,
May 18, 2008, 7:07:43 PM5/18/08
to WellR...@googlegroups.com
>>
>>> ::Net::SMTP is what you're after.
>>
>> I know. If you look at my code you'll see that I am trying to use
>> NET::SMTP from inside of my module Tim::Net.
>
> You asked how to specify a Net module that's at the root not insude
> Tim. You prefix it with those two colons :)
>
> ::Net::SMTP


Ah sorry. I get it now.

Tim Carey-Smith

unread,
May 18, 2008, 6:50:47 PM5/18/08
to WellR...@googlegroups.com
On 19/05/2008, at 10:47 AM, Tim Uckun wrote:

>
> This is not a rails question per se but it's a ruby question.
>
> I am having some namespace issues. For example I have the following.
>
>

> This causes an error because the code thinks Net::SMTP should be in my
> module. How do I refer to the SMTP in the Net library explicitly?

You can access a top-level constant by using ::Net::SMTP.

Helpful?

Tim

Reply all
Reply to author
Forward
0 new messages