Hi there,
whilst looking at the Rails source code, I noticed a number of
methods that followed this pattern:
def initialize
#some initialisation code
super
end
I thought that the appropriate way to do this was to call super
before the init stuff, just in case the parent init code overrode the
current init code.
Consider the following:
>> class First
>> def cheese
>> puts @cheese
>> end
>> def initialize
>> @cheese = ''Stilton''
>> end
>> end
=> nil
>> First.new.cheese
Stilton
=> nil
>> class Second < First
>> def initialize
>> @cheese = ''Shropshire Blue''
>> super
>> end
>> end
=> nil
>> Second.new.cheese
Stilton
=> nil
>> class Third < First
>> def initialize
>> super
>> @cheese = ''Cheshire''
>> end
>> end
=> nil
>> Third.new.cheese
Cheshire
However, the occasional call to super at the end of a method may be
because you desire this behaviour, I''m just being inquisitive :-)
An example where I imagine the super would be better placed at the
beginning of the method:
(actionpack/lib/action_controller/cgi_process.rb)
def initialize(cgi, session_options = {})
@cgi = cgi
@session_options = session_options
@env = @cgi.send(:env_table)
super()
end
Cheers,
Sam Aaron
---
http://sam.aaron.name
P.S.
For the interested here''s the result of a quick search in a frozen
Rails 1.2.2 app:
(I marked the super calls with a prefix b/e/o/m just so i could
retrieve the location/line number via grep)
All initialize methods that start with a call to super:
./actionmailer/test/mail_render_test.rb:19: bsuper
./actionpack/lib/action_controller/base.rb:21: bsuper(message)
./actionpack/lib/action_controller/flash.rb:56: bsuper
./actionpack/lib/action_controller/routing.rb:610: bsuper()
./actionpack/lib/action_controller/routing.rb:641: bsuper(value)
./actionpack/lib/action_controller/routing.rb:655: bsuper()
./actionpack/lib/action_controller/vendor/html-scanner/html/node.rb:
7: bsuper()
./actionpack/lib/action_controller/vendor/html-scanner/html/node.rb:
213: bsuper(parent, line, pos)
./actionpack/lib/action_controller/vendor/html-scanner/html/node.rb:
290: bsuper(parent, line, pos)
./actionwebservice/lib/action_web_service/client/soap_client.rb:
47: bsuper(api, endpoint_uri)
./actionwebservice/lib/action_web_service/support/signature_types.rb:
184: bsuper(spec, Array, name)
./actionwebservice/setup.rb:159: bsuper
./actionwebservice/setup.rb:179: bsuper name, template, default, desc
./actionwebservice/setup.rb:933: bsuper
./actionwebservice/test/abstract_dispatcher.rb:12: bsuper(*args)
./activerecord/lib/active_record/associations/
has_and_belongs_to_many_association.rb:5: bsuper
./activerecord/lib/active_record/associations/has_many_association.rb:
5: bsuper
./activerecord/lib/active_record/associations/
has_many_through_association.rb:5: bsuper
./activerecord/lib/active_record/associations/has_one_association.rb:
5: bsuper
./activesupport/lib/active_support/core_ext/load_error.rb:4: bsuper
(message)
./railties/lib/rails_generator/base.rb:203: bsuper
./railties/lib/rails_generator/generators/applications/app/
app_generator.rb:13: bsuper
./railties/lib/rails_generator/generators/components/resource/
resource_generator.rb:14: bsuper
./railties/lib/rails_generator/generators/components/scaffold/
scaffold_generator.rb:52: bsuper
./railties/lib/rails_generator/lookup.rb:174: bsuper label
All initialize methods that have a call to super embedded within the
method:
./actionpack/lib/action_controller/cookies.rb:43: msuper()
./activerecord/lib/active_record/associations.rb:1444:
msuper(reflection.klass)
./activerecord/lib/active_record/connection_adapters/
firebird_adapter.rb:49: msuper(name.downcase, nil,
@firebird_type, !null_flag)
./activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:
92: msuper
./activesupport/lib/active_support/core_ext/hash/
indifferent_access.rb:7: msuper()
./activesupport/lib/active_support/core_ext/hash/
indifferent_access.rb:10: msuper(constructor)
./activesupport/lib/active_support/vendor/builder/xmlmarkup.rb:
187: msuper(indent, margin)
./railties/lib/rails_generator/generators/components/plugin/
plugin_generator.rb:6: msuper
All initialize methods where the call to super is the only method call:
./actionpack/lib/action_controller/base.rb:37: osuper(message ||
DEFAULT_MESSAGE)
./actionpack/lib/action_controller/base.rb:44: osuper(message ||
DEFAULT_MESSAGE)
./actionpack/lib/action_controller/base.rb:51: osuper(message ||
DEFAULT_MESSAGE)
./actionpack/lib/action_controller/integration.rb:444: osuper
(name.to_s)
./actionpack/lib/action_view/helpers/prototype_helper.rb:793:
osuper(generator, @pattern = pattern)
./actionpack/lib/action_view/helpers/prototype_helper.rb:870:
osuper(generator, "$$(#{pattern.to_json})")
./activerecord/lib/active_record/associations.rb:14: osuper
("Could not find the association #{reflection.options
[:through].inspect} in model #{owner_class_name}")
./activerecord/lib/active_record/associations.rb:20: osuper
("Cannot have a has_many :through association
''#{owner_class_name}##
{reflection.name}'' on the polymorphic object ''#
{source_reflection.class_name}##{source_reflection.name}''.")
./activerecord/lib/active_record/associations.rb:43: osuper
("Cannot associate new records through ''#{owner.class.name}##
{reflection.name}'' on
''#{reflection.source_reflection.class_name
rescue nil}##{reflection.source_reflection.name rescue nil}''. Both
records must have an id in order to create the has_many :through
record associating them.")
./activerecord/lib/active_record/associations.rb:49: osuper("Can
not eagerly load the polymorphic association #
{reflection.name.inspect}")
./activerecord/lib/active_record/associations.rb:55: osuper("Can
not add to a has_many :through association. Try adding to #
{reflection.through_reflection.name.inspect}.")
./activerecord/lib/active_record/migration.rb:7: osuper
("Multiple migrations have the version number #{version}")
./railties/lib/rails_generator/lookup.rb:193: osuper :RubyGems
All initialize methods that have a call to super at the end of the
method:
./actionmailer/lib/action_mailer/vendor/tmail/port.rb:50: esuper()
./actionmailer/lib/action_mailer/vendor/tmail/port.rb:301: esuper()
./actionpack/lib/action_controller/cgi_ext/cookie_performance_fix.rb:
53: esuper(@value)
./actionpack/lib/action_controller/cgi_process.rb:47: esuper()
./actionpack/lib/action_controller/cgi_process.rb:181: esuper()
./actionpack/lib/action_controller/integration.rb:205:
esuper()
./actionpack/lib/action_controller/test_process.rb:36: esuper()
./actionpack/lib/action_view/helpers/prototype_helper.rb:729:
esuper(generator, "$(#{id.to_json})")
./actionpack/lib/action_view/helpers/prototype_helper.rb:765:
esuper(generator)
./actionpack/test/controller/deprecated_instance_variables_test.rb:
7: esuper()
./actionpack/test/controller/webservice_test.rb:13: esuper()
./activerecord/lib/active_record/associations.rb:29: esuper
("Could not find the source association(s) #
{source_reflection_names.collect(&:inspect).to_sentence :connector =>
''or''} in model #{through_reflection.klass}. Try
''has_many #
{reflection.name.inspect}, :through => #
{through_reflection.name.inspect}, :source => <name>''. Is it
one of #
{source_associations.to_sentence :connector =>
''or''}?")
./activerecord/lib/active_record/associations.rb:37: esuper
("Invalid source reflection macro :#{source_reflection.macro}#
{" :through" if source_reflection.options[:through]} for has_many #
{reflection.name.inspect}, :through => #
{through_reflection.name.inspect}. Use :source to specify the source
reflection.")
./activerecord/lib/active_record/connection_adapters/
frontbase_adapter.rb:75: esuper(string)
./activerecord/lib/active_record/connection_adapters/
oracle_adapter.rb:621: esuper @connection
./activerecord/lib/active_record/validations.rb:13: esuper
("Validation failed: #{@record.errors.full_messages.join(",
")}")
./activerecord/lib/active_record/vendor/mysql.rb:1070: esuper error
./railties/lib/commands/ncgi/listener:17: esuper()
./railties/lib/rails_generator/generators/components/
session_migration/session_migration_generator.rb:4: esuper
./railties/lib/webrick_server.rb:72: esuper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---