Open-uri and Kernel.open

(Scott) #1

I am new ruby usrs (and loving it) and have a question.

By "requiring" opne-uri into my source file I can now reference "http"
addresses as they were local files. It seems that open-uri "hooks"
into the Kernel.open method.

I am just wondering what aspect of the ruby language allows you to do
this. Or is there some mechanism inside of the Kernel that allows you
to register a different protocol?

thanks in advance, scott.

(Mohit Muthanna) #2

The open-uri module simply redefines the Kernel.open method. You can
do it yourself:

module Kernel
  private
  alias original_open open

  def open( blah, *blah_params, &blah_block)
     ...blah
     if some_condition
        original_open( blah, *blah_params, &blah_block)
     end
  end

  module_function :open
end

Above, we store the original "open" method in original_open; then we
redefine open, calling the original open function if necessary.

HTH,
Mohit.

···

On 8/19/05, Scott <scott.walter@vivare.com> wrote:

I am new ruby usrs (and loving it) and have a question.

By "requiring" opne-uri into my source file I can now reference "http"
addresses as they were local files. It seems that open-uri "hooks"
into the Kernel.open method.

I am just wondering what aspect of the ruby language allows you to do
this. Or is there some mechanism inside of the Kernel that allows you
to register a different protocol?

thanks in advance, scott.

--
Mohit Muthanna [mohit (at) muthanna (uhuh) com]
"There are 10 types of people. Those who understand binary, and those
who don't."

(Scott) #3

Thanks for the info. Thats a cool feature of Ruby where you can
redfine existing methods.
Mohit Muthanna wrote:

···

The open-uri module simply redefines the Kernel.open method. You can
do it yourself:

module Kernel
  private
  alias original_open open

  def open( blah, *blah_params, &blah_block)
     ...blah
     if some_condition
        original_open( blah, *blah_params, &blah_block)
     end
  end

  module_function :open
end

Above, we store the original "open" method in original_open; then we
redefine open, calling the original open function if necessary.

HTH,
Mohit.

On 8/19/05, Scott <scott.walter@vivare.com> wrote:
> I am new ruby usrs (and loving it) and have a question.
>
> By "requiring" opne-uri into my source file I can now reference "http"
> addresses as they were local files. It seems that open-uri "hooks"
> into the Kernel.open method.
>
> I am just wondering what aspect of the ruby language allows you to do
> this. Or is there some mechanism inside of the Kernel that allows you
> to register a different protocol?
>
> thanks in advance, scott.
>
>
>

--
Mohit Muthanna [mohit (at) muthanna (uhuh) com]
"There are 10 types of people. Those who understand binary, and those
who don't."

(Robert) #4

Scott wrote:

Thanks for the info. Thats a cool feature of Ruby where you can
redfine existing methods.

One of the most widely used features... :slight_smile:

Mohit Muthanna wrote:

The open-uri module simply redefines the Kernel.open method. You can
do it yourself:

module Kernel
  private
  alias original_open open

  def open( blah, *blah_params, &blah_block)
     ...blah
     if some_condition
        original_open( blah, *blah_params, &blah_block)
     end
  end

Btw, you don't need this line:

  module_function :open
end

Kind regards

    robert