Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I came across this code which is about dropbox

Tags:

ruby

Can anybody explain def self.extended(base), what does it mean here or any idea?

module Paperclip
  module Storage
    module Dropbox
      def self.extended(base)
        base.instance_eval do
          @options[:dropbox_options] ||= {}
          @options[:path] = nil if @options[:path] == 
          self.class.default_options[:path]
          @options[:dropbox_visibility] ||= "public"
          @path_generator = PathGenerator.new(self, @options)
          #dropbox_client # Force creation of dropbox_client
        end
      end
    end
  end
end
like image 909
ALOK GOUR Avatar asked Dec 08 '25 19:12

ALOK GOUR


1 Answers

The self.extended method is called when the module is extended. It allows methods to be executed in the context of the base (where the module is extended).

You can try it yourself and understand this with a simple example code. Just paste in a file ruby file and run it.

Example for self.extended

module A
  def self.extended(base)
    puts "#{self} extended in #{base}"
  end 
end

class Apple
  extend A
end

# This should print: "A extended in Apple"

Example for self.included

module A
  def self.included(base)
    puts "#{self} included in #{base}"
  end 
end

class Apple
  include A
end

# This should print: "A included in Apple"

You can read more here: http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/

like image 53
Rush0312 Avatar answered Dec 11 '25 14:12

Rush0312



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!