Superclass Mismatch, Struct, Reloading and Spork

Best Way to Abstract Initializing Attributes

A solution to that problem already (partially) exists, but if you want a more declarative approach in your classes then the following should work.

class Class
def initialize_with(*attrs, &block)
attrs.each do |attr|
attr_accessor attr
end
(class << self; self; end).send :define_method, :new do |*args|
obj = allocate
init_args, surplus_args = args[0...attrs.size], args[attrs.size..-1]
attrs.zip(init_args) do |attr, arg|
obj.instance_variable_set "@#{attr}", arg
end
obj.send :initialize, *surplus_args
obj
end
end
end

You can now do:

class MyClass < ParentClass
initialize_with :foo, :bar
def initialize(baz)
@initialized = true
super(baz) # pass any arguments to initializer of superclass
end
end
my_obj = MyClass.new "foo", "bar", "baz"
my_obj.foo #=> "foo"
my_obj.bar #=> "bar"
my_obj.instance_variable_get(:@initialized) #=> true

Some characteristics of this solution:

  • Specify constructor attributes with initialize_with
  • Optionally use initialize to do custom initialization
  • Possible to call super in initialize
  • Arguments to initialize are the arguments that were not consumed by attributes specified with initialize_with
  • Easily extracted into a Module
  • Constructor attributes specified with initialize_with are inherited, but defining a new set on a child class will remove the parent attributes
  • Dynamic solution probably has performance hit

If you want to create a solution with absolute minimal performance overhead, it would be not that difficult to refactor most of the functionality into a string which can be evaled when the initializer is defined. I have not benchmarked what the difference would be.

Note: I found that hacking new works better than hacking initialize. If you define initialize with metaprogramming, you'd probably get a scenario where you pass a block to initialize_with as a substitute initializer, and it's not possible to use super in a block.

TypeError: superclass mismatch for class Word in Ruby

A thumb rule for irb (either way irb or rails console)

If you are creating the same class twice with inheritance (superclass), exit the irb instance and create it again. Why this? Because otherwise class conflicts will happen.

In your case, you are using Windows (found from the question), so just type exit on DOS prompt and again type irb or rails console and create your Word class and it should work. Please let me know if it doesn't work for you.

Rails 4: Superclass mismatch exception in production environment that can't be

The Superclass mismatch error happens when there is a re-definition of a class. According to your question, the error is on BaseController. Here's my theory looking at what you've posted. BaseController is being defined more than once, so that when this code is loaded the BaseController is ambiguous:

module Engine1
class Global::ConfigurationsController < Global::BaseController
end
end

It appears that at load time there are 2 definitions extant of BaseController.

# app/controllers/global/base_controller.rb
class Global::BaseController < ApplicationController
end

# app/controllers/global/configurations_controller.rb
class Global::BaseController < Global::BaseController
end

So which BaseController should be loaded? To you it is obvious what should be done here, and I think I understand what you're trying to do with adding a level of indirection to your configuration. But Rails sees these two definitions as the same name at load time and throws the superclass error.


These are my thoughts about your question. I'm curious what you think.

`module:Rails': superclass mismatch for class Server (TypeError)

A better way to do what you want:

require 'rails/commands/server'

module DefaultOptions
def default_options
super.merge!(Port: 8081)
end
end

Rails::Server.prepend(DefaultOptions)

The reason for the error message is because you are attempting to redefine the Rails::Server class and changing it's inheritance structure. Rails::Server inherits from ::Rack::Server, however your code is trying to say it no longer does. Therefore, you get your superclass mismatch error.

Rails3 not reloading code in lib while in development mode

They removed the lib folder the app root in Rails 3.

You can either add it back

config.autoload_paths << 'lib'
or you can use `require_dependency` in your helper.

module FooBarHelper
require_dependency 'foo/bar'

def test_foo_bar
fb = Foo::Bar.new
fb.test
end
end

Both ways tell Rails that your file lib/foo/bar.rb should be autoloaded and subsequently, reloaded each request.



Related Topics



Leave a reply



Submit