Using Forwardable in Ruby

By: James Edward Gray II Emailed: 1698 times Printed: 2203 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Forwardable makes building a new class based on existing work, with a proper interface, almost trivial. We want to rely on what has come before obviously, but with delegation we can take just the methods we need and even rename them as appropriate. In many cases this is preferable to inheritance, which gives us the entire old interface, even if much of it isn’t needed.

  class Queue
    extend Forwardable

    def initialize
      @q = [ ]    # prepare delegate object
    end

    # setup prefered interface, enq() and deq()...
    def_delegator :@q, :push, :enq
    def_delegator :@q, :shift, :deq

    # support some general Array methods that fit Queues well
    def_delegators :@q, :clear, :first, :push, :shift, :size
  end

  q = Queue.new
  q.enq 1, 2, 3, 4, 5
  q.push 6

  q.shift    # => 1
  while q.size > 0
    puts q.deq
  end

  q.enq "Ruby", "Perl", "Python"
  puts q.first
  q.clear
  puts q.first

Prints:

  2
  3
  4
  5
  6
  Ruby
  nil

SingleForwardable

   printer = String.new
   printer.extend SingleForwardable        # prepare object for delegation
   printer.def_delegator "STDOUT", "puts"  # add delegation for STDOUT.puts()
   printer.puts "Howdy!"

Prints:

   Howdy!

Ruby Home | All Ruby Tutorials | Latest Ruby Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by James Edward Gray II
Using Forwardable in Ruby
Using Generator in Ruby
Using GServer in Ruby
Sending emails using SMTP in Ruby
Reading emails using POP3 in Ruby
IMAP in Ruby

More Tutorials in Ruby
Standard Library Packages in Ruby
Module Abbrev in Ruby
encoding and decoding in Ruby using Base64 Module
Benchmark module in Ruby
benchmark(caption = "", label_width = nil, fmtstr = nil, *labels) {|report| ...} in Ruby
bm(label_width = 0, *labels) {|report| ...} in Ruby
bmbm(width = 0) {|job| ...} in Ruby
CGI class in Ruby
URL encode a string in Ruby
escapeElement() in Ruby
escapeHTML() in Ruby
new(type = "query") in Ruby
parse(query) in Ruby
pretty() in Ruby
Format a Time object as a String in Ruby

More Latest News
Most Viewed Articles (in Ruby )
Methods in Ruby on rails
if . . . elsif . . . else in Ruby on rails
bm(label_width = 0, *labels) {|report| ...} in Ruby
URL encode a string in Ruby
URL decode a string in Ruby
SimpleDelegator in Ruby
Using Forwardable in Ruby
Sending emails and Receiving emails using Ruby On rails
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
Naming conventions in Ruby on rails
Command-line Arguments in Ruby on rails
unless in Ruby on rails
The Question Mark Operator in Ruby on rails
Standard Library Packages in Ruby
Most Emailed Articles (in Ruby)
parse(query) in Ruby
POST a form in Ruby
IMAP in Ruby
Sending emails using SMTP in Ruby
Defining tables and classes (using MySQL) in Ruby on Rails
Action Pack - Ruby On rails from request to response
Getting started with Ruby on rails
Prompts, Command Lines, Prompts, and irb in Ruby on rails
unless in Ruby on rails
escapeHTML() in Ruby
new(type = "query") in Ruby
date and time in Ruby
SimpleDelegator in Ruby
Reading URL content using Ruby (HTTP)
Naming conventions in Ruby on rails