Subsecciones


super

class Person
  def initialize name, dob #dob = date of birth
    @name = name
    @dob = dob
  end

  def age
    (Time.now - @dob) / (365 * 24 * 60 * 60)
  end

  def introduce_yoself
    print "Hey, I'm #{@name} and I was born #{age} years ago.\n"
  end
end

maxxx = Person.new("Max", Time.new(1988, 9, 11))
maxxx.introduce_yoself
# which prints
# "Hey, I'm Max and I was born 23.986193869358075 years ago."

class CoolDude < Person
  def initialize name, dob, nickname
    super name, dob
    @nickname = nickname
  end

  def introduce_yoself
    print "Yo, they call me '#{@nickname}'."
  end
end

waltr0x = CoolDude.new("Walter", Time.new(1987, 9, 29), "The Laser")
# we've still got this function
print waltr0x.age
# 24.939627905041938
waltr0x.introduce_yoself
# "Yo, they call me 'The Laser'."

  1. The Ruby-reserved word super is a special kind of method invocation expression. This keyword is used when creating a subclass of another class.
  2. By itself, super passes the arguments of the current method to the method with the same name in the superclass.
  3. If you use super as a bare keyword—with no arguments and no parentheses—then all of the arguments that were passed to the current method are passed to the superclass method.
  4. As with normal method invocations, the parentheses around super arguments are optional. Because a bare super has special meaning, however, you must explicitly use a pair of empty parentheses if you want to pass zero arguments from a method that itself has one or more arguments.

Ejemplo: Heredando de GServer

GServer implements a generic server, featuring thread pool management, simple logging, and multi-server management.

Any kind of application-level server can be implemented using this class. It accepts multiple simultaneous connections from clients, up to an optional maximum number.

Several services (i.e. one service per TCP port) can be run simultaneously, and stopped at any time through the class method GServer.stop(port).

All the threading issues are handled, saving you the effort. (See Rediscovering Ruby: GServer)

[~/rubytesting/inheritance]$ cat gserver_example.rb 
# http://www.aimred.com/news/developers/2010/01/16/rediscovering_ruby_gserver/
require 'gserver'

class TimeServer < GServer
  def initialize(port=10001, host='0.0.0.0', *args)
    super
  end
  def serve(io)
    #  line = io.readline
    io.puts(Time.now.to_s)
  end
end

server = TimeServer.new
server.stdlog = $stderr
server.audit = true 
server.debug = true 
server.start

server.join

[~/rubytesting/inheritance]$ cat gserver_client.rb 
require 'socket'

echo = TCPSocket.open( 'localhost', 10001 )
#echo.puts( "Hello\nWorld" )
puts( echo.readline )

[~/rubytesting/inheritance]$ ruby gserver_example.rb 
[Mon Oct 27 11:32:23 2014] TimeServer 0.0.0.0:10001 start
[Mon Oct 27 11:32:34 2014] TimeServer 0.0.0.0:10001 client:52492 127.0.0.1<127.0.0.1> connect
[Mon Oct 27 11:32:34 2014] TimeServer 0.0.0.0:10001 client:52492 disconnect

[~/rubytesting/inheritance]$ ruby gserver_client.rb 
2014-10-27 11:32:34 +0000

Casiano Rodriguez León 2015-06-18