Class initialization with open method

Is it possible that If I put a method to open a webpage in the class
initialization that when I call it like content = Web.New then it
automatically assigns the contents of the page?
And how exactly would it be done if not?

On Sat, Jan 4, 2014 at 6:54 PM, Greg H. [email protected] wrote:

Is it possible that If I put a method to open a webpage in the class
initialization that when I call it like content = Web.New then it
automatically assigns the contents of the page?
And how exactly would it be done if not?

Since you picked the name “New” for the method nothing special needs
to be done. Just implement it and that’s it.

Generally pulling web content is a quite expensive task (in terms of
time that it takes and potential issues). It’s usually regarded best
to not do that in a constructor. It’s usually better to implement the
constructor in a way that instances are properly initialized. Loading
web content can then be done by another method. If you want to provide
the combined functionality you can always define an additional method
e.g.

class Web
def self.load
new.tap do |item|
item.load_whatever
end
end

def load_whatever

end
end

Btw. the name “Web” for the class seems a bit unspecific. You
certainly won’t implement the whole web in a single class. :slight_smile:

You could also make use of gem Mechanize to handle web content -
depending on what you do.

Kind regards

robert

On 14-01-04, 9:54, Greg H. wrote:

Is it possible that If I put a method to open a webpage in the class
initialization that when I call it like content = Web.New then it
automatically assigns the contents of the page?
And how exactly would it be done if not?

Yes, you could do that in the initialize method. Better would be to do
it in a more lazy way so you’re not forced to make an HTTP connection
just to get an object:

 class Web
   def initialize
     # nothing
   end

   def contents
     @contents ||= Net::HTTP.get(@uri)
   end
 end

…for example.

2014/1/5 Robert K. [email protected]

Btw. the name “Web” for the class seems a bit unspecific. You
certainly won’t implement the whole web in a single class. :slight_smile:

why the Web class is an unspecific? would you mind if you give me some
example about this?

thankyou ,

On Fri, Jan 10, 2014 at 5:33 PM, Bayu Aldi Yansyah
[email protected] wrote:

2014/1/5 Robert K. [email protected]

Btw. the name “Web” for the class seems a bit unspecific. You
certainly won’t implement the whole web in a single class. :slight_smile:

why the Web class is an unspecific? would you mind if you give me some
example about this?

“Web” is just too generic a name. In your case you seem to want to
access only part of the web - namely a specific website.

Kind regards

robert

2014/1/11 Robert K. [email protected]

why the Web class is an unspecific? would you mind if you give me some
example about this?

“Web” is just too generic a name. In your case you seem to want to
access only part of the web - namely a specific website.

oh, i understand now. thankyou :slight_smile:

bayu
regards,