~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/forwardable.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
#   forwardable.rb -
3
 
#       $Release Version: 1.1$
4
 
#       $Revision: 31833 $
5
 
#       by Keiju ISHITSUKA(keiju@ishitsuka.com)
6
 
#       original definition by delegator.rb
 
3
#       $Release Version: 1.1$
 
4
#       $Revision: 31685 $
 
5
#       by Keiju ISHITSUKA(keiju@ishitsuka.com)
 
6
#       original definition by delegator.rb
7
7
#       Revised by Daniel J. Berger with suggestions from Florian Gross.
8
8
#
9
9
#       Documentation by James Edward Gray II and Gavin Sinclair
84
84
#      def_delegator :Implementation, :service
85
85
#
86
86
#      class Implementation
87
 
#         def service...
 
87
#         def service...
88
88
#      end
89
89
#    end
90
90
#
175
175
    end
176
176
  end
177
177
 
 
178
  # Define +method+ as delegator instance method with an optional
 
179
  # alias name +ali+. Method calls to +ali+ will be delegated to
 
180
  # +accessor.method+. 
 
181
  #
 
182
  #   class MyQueue
 
183
  #     extend Forwardable
 
184
  #     attr_reader :queue
 
185
  #     def initialize
 
186
  #       @queue = []
 
187
  #     end
 
188
  #     
 
189
  #     def_delegator :@queue, :push, :mypush
 
190
  #   end
 
191
  #
 
192
  #   q = MyQueue.new
 
193
  #   q.mypush 42
 
194
  #   q.queue    #=> [42]
 
195
  #   q.push 23  #=> NoMethodError
 
196
  #
178
197
  def def_instance_delegator(accessor, method, ali = method)
179
198
    line_no = __LINE__; str = %{
180
199
      def #{ali}(*args, &block)
181
 
        begin
182
 
          #{accessor}.__send__(:#{method}, *args, &block)
183
 
        rescue Exception
184
 
          $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
185
 
          ::Kernel::raise
186
 
        end
 
200
        begin
 
201
          #{accessor}.__send__(:#{method}, *args, &block)
 
202
        rescue Exception
 
203
          $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
 
204
          ::Kernel::raise
 
205
        end
187
206
      end
188
207
    }
189
208
    # If it's not a class or module, it's an instance
246
265
  # provided, it is used as the name for the delegate method.
247
266
  #
248
267
  def def_single_delegator(accessor, method, ali = method)
249
 
    line_no = __LINE__; str = %{
 
268
    str = %{
250
269
      def #{ali}(*args, &block)
251
 
        begin
252
 
          #{accessor}.__send__(:#{method}, *args, &block)
253
 
        rescue Exception
254
 
          $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
255
 
          ::Kernel::raise
256
 
        end
 
270
        begin
 
271
          #{accessor}.__send__(:#{method}, *args, &block)
 
272
        rescue Exception
 
273
          $@.delete_if{|s| %r"#{Regexp.quote(__FILE__)}"o =~ s} unless Forwardable::debug
 
274
          ::Kernel::raise
 
275
        end
257
276
      end
258
277
    }
259
278
 
264
283
  alias def_delegators def_single_delegators
265
284
  alias def_delegator def_single_delegator
266
285
end
267
 
 
268
 
 
269
 
 
270