Class initialization with 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?

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On 14-01-04, 9:54, Greg Hajdu 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 Sat, Jan 4, 2014 at 6:54 PM, Greg Hajdu <lists@ruby-forum.com> 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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

thankyou ,

···

2014/1/5 Robert Klemme <shortcutter@googlemail.com>

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:

--
Bayu Aldi Yansyah,
Universitas Airlangga

"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

···

On Fri, Jan 10, 2014 at 5:33 PM, Bayu Aldi Yansyah <bayualdiyansyah@gmail.com> wrote:

2014/1/5 Robert Klemme <shortcutter@googlemail.com>

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?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

oh, i understand now. thankyou :slight_smile:

bayu
regards,

···

2014/1/11 Robert Klemme <shortcutter@googlemail.com>

On Fri, Jan 10, 2014 at 5:33 PM, Bayu Aldi Yansyah > <bayualdiyansyah@gmail.com> wrote:
>
> 2014/1/5 Robert Klemme <shortcutter@googlemail.com>
>>
>>
>> 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.

--
Bayu Aldi Yansyah,
Universitas Airlangga