Forum: Ruby on Rails RE: Help mixin in class methods

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1f840b624a948a3f39299af25620121b?d=identicon&s=25 Ross Dawson (Guest)
on 2006-05-18 04:14
(Received via mailing list)
Saw this
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/...

so

module Foo
  module ClassMethods
    def bar
      puts 'hello from class method bar'
    end
  end

  include ClassMethods

  def self.append_features(klass)
    super
    klass.extend(ClassMethods)
  end
end

class FooBar
  include Foo
end

FooBar.bar

>hello from class method bar

HTH

Ross
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-05-18 17:23
(Received via mailing list)
Thanks for this. Couldn't make it work, but it gave me the energy to
give it another go, and with the help of the Pickaxe, managed to make it
work.

What I did (in case anyone else needs it) is split the module into two
submodules, then include the submodule containing the instance methods,
and extend [the class with] the one containing the class methods. So it
looked like this:
[lib/trust_ranking.rb]
module TrustRanking
    module ClassMethods
        def shinynewclassmethod
            # stuff here
       end

       #more methods
    end

    module InstanceMethods
        def shinynewinstancemethod
            # stuff here
       end

       #more methods
    end
end

class User < ActiveRecord::Base
  include TrustRanking::InstanceMethods
  extend TrustRanking::ClassMethods
#
end

Thanks again for your help.
Chris
B7ae1a166a9f42a2cb7e87fda8f811e2?d=identicon&s=25 Jim mack (Guest)
on 2006-06-22 03:34
(Received via mailing list)
I get "uninitialized constant ClassMethods" when I try this:

require "sort_order"

class Player < ActiveRecord::Base
  include SortOrder::ClassMethods
  tester
end

module SortOrder
  module ClassMethods
    def tester
      @test = 2
    end
  end

  module InstanceMethods
    def testers
      @test = 35
    end
  end
end

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`const_missing'
#{RAILS_ROOT}/app/models/player.rb:5
#{RAILS_ROOT}/app/controllers/players_controller.rb:13:in `list'
-e:4

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`const_missing'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:140:in
`load'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:56:in
`require_or_load'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:30:in
`depend_on'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:85:in
`require_dependency'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:98:in
`const_missing'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in
`const_missing'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/inflector.rb:161:in
`constantize'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/string/inflections.rb:59:in
`constantize'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/pagination.rb:194:in
`paginator_and_collection_for'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/pagination.rb:129:in
`paginate'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`perform_action_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
script/server:3

C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
`const_missing'
#{RAILS_ROOT}/app/models/player.rb:5
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:140:in
`load'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:56:in
`require_or_load'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:30:in
`depend_on'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:85:in
`require_dependency'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:98:in
`const_missing'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:131:in
`const_missing'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/inflector.rb:161:in
`constantize'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/string/inflections.rb:59:in
`constantize'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/pagination.rb:194:in
`paginator_and_collection_for'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/pagination.rb:129:in
`paginate'
#{RAILS_ROOT}/app/controllers/players_controller.rb:13:in `list'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:910:in
`perform_action_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:368:in
`perform_action_without_benchmark'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/benchmarking.rb:69:in
`perform_action_without_rescue'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/rescue.rb:82:in
`perform_action'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/base.rb:381:in
`process_without_filters'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/filters.rb:377:in
`process_without_session_management_support'
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action_controller/session_management.rb:117:in
`process'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/dispatcher.rb:38:in
`dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:115:in
`handle_dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:81:in
`service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/webrick_server.rb:67:in
`dispatch'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/servers/webrick.rb:59
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.1.2/lib/commands/server.rb:30
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in
`require'
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:147:in
`require'
script/server:3
-e:4

This error occured while loading the following files:
   player.rb
   sort_order/class_methods.rb

So, why does it try for a secondary file?
This topic is locked and can not be replied to.