RuNubie here. I’ve got a class Login that logs into gmail using the net/IMAP library. What is happening is that I create a new instance of that class, such as:
a = Login.new("username", "gmail.com", "passw")
Then, I’m working on other classes that will do some “stuff” with the mailbox. The problem is that the @imap variable I’ve defined in Login seems to have disappeared (due to scoping I assume).
This is how @imap is declared in Login class:
@imap = Net::IMAP.new('imap.gmail.com',993,true,nil,false)
So this:
@today = Date.today
@received_today = imap.search(["SINCE", @today.strftime("%d-%b-%Y")]).count.to_s
…returns an error. These are the two errors I’ve gotten while playing around with this. The first one is when I use imap, the second one is when I try @imap:
NameError: undefined local variable or method `imap' for #<Object:0x10718d2a8>
NoMethodError: undefined method `search' for nil:NilClass
What are the best practices for dealing with a situation like this? Is the only solution to define my methods that do “stuff” in the same class where I’m creating the new instance of Net::IMAP? Is declaring @imap as a global variable $imap a bad practice? So confused, I bet the answer is very simple and obvious too, but I’m just not seeing it. Thanks!
This:
won’t work because, well, there is no
imapin scope at that point and so you get a NameError. When you try it like this:You get a NoMethodError because instance variables, such as
@imap, are automatically created at first use and initialized asnil. Your real@imapis in another object so you can’t refer to it as@imapanywhere else.I think you want a structure more like this:
Keep your
Net::IMAPlocalized inside your User and let other objects use it by providing a simple accessor method.Oh and that global
$imapidea, I’ll just pretend I didn’t see that as globals are almost always a really bad idea.