Calling super on a method defined by define_method

I have created a Model class where I define methods based on a method (attribute) called in User (which inherits from Model). The problem is that I cannot override the method defined by define_method, and call super to pass to the defined method. I guess this is because the defined method is added to User itself, and not to the Model, so it actually has no method in the superclass (i.e. Model).

The reason I want to do this is because most attributes should be saved to the database directly, while some attributes, like password, needs some additional processing.

class Model
  def self.attribute(name)
    define_method(name) do
      self
    end
  end  
end

class User < Model
  attribute :password
end

class User2 < Model
  attribute :password

  def password
    super
  end
end

@user = User.new
puts @user.password # => <User:0x00000100845540>

@user2 = User2.new
puts @user2.password
# define_super.rb:17:in `password': super: no superclass method 
# `password' for #<User2:0x00000100845578> (NoMethodError)
# from define_super.rb:25:in `<main>'

Is there any way I could change the code to get this working? I need a way to override dynamically created methods.


Define the method on the superclass:

class Model
  def self.attribute(name)
    superclass.send :define_method, name do
      self
    end
  end  
end

Calling super on a method defined by define_method, Calling super on a method defined by define_method. I have created a Model class where I define methods based on a  The super() function returns a temporary object of the superclass that then allows you to call that superclass’s methods. How to call super in Python 3. We can call using the following syntax. We will take a regular class definition and modify it by adding the super function. The final code with a super() keyword looks like below.


The way Rails deals with this is that there are multiple ways to get attributes. One of these is (by convention) never overridden so it can be used in your defined methods:

# This method is never overridden, but also rarely used as a public method
def[](key)
  # Returns value of `key` attribute
end

# This is the effective default implementation of an attribute
def att1
  self[:att1]
end

# This shows how you can have custom logic but still access the underlying value
def att2
  self[:att2] unless self[:att2].empty?
end

Why I can not call super in define_method with overloading method , When I run code below it raise error: implicit argument passing of super from method defined by  In simple words, it is used to refer to its immediate super-class or parent-class. It is considered a better method to access data members and member functions of the base class, rather than using the base class name itself. super() method in function overriding is one of the most common applications of this new-class method. Syntax: super(<own


Using superclass.send :define_method might lead to some unexpected behaviour, because that method would be available to every Model subclasses.

It is well described in this article https://thepugautomatic.com/2013/07/dsom/

So following this article you could define your attribute method like this

class Model
  MODULE_NAME = :DynamicAttributes

  def self.attribute(name)
    if const_defined?(MODULE_NAME, _search_ancestors = false)
      mod = const_get(MODULE_NAME)
    else
      mod = const_set(MODULE_NAME, Module.new)
      include mod
    end

    mod.module_eval do
      define_method(name) do
        self
      end
    end
  end
end

Method override with super() (Example), It is common to not know if a method has already been defined. By calling the super without any params Ruby will get all previous params If you are using the define_method() signature this params shortcut won't work. One statement specifically for use in the method-body of an instance is a call to an instance method defined in a super class using the SUPER system reference. Also, in the method-body of a static method, you can only access other static members defined within the current class hierarchy in addition to local method data; instance members defined within the current class hierarchy are inaccessible.


Dynamic super-overridable methods in Ruby – The Pug Automatic, When you want to override methods defined by a library, super is more define_method(name) do up the inheritance chain, we could define our own method and just call super . A method is a group of statements that together perform a task. Every C# program has at least one class with a method named Main. To use a method, you need to − Define the method; Call the method; Defining Methods in C#. When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as


The super keyword in Ruby - rubycademy, Within the Child#say method, we call super without any argument. So, Ruby tries to find a method #say in The Parent class doesn't define this method. So super tries to find the #say  super is a keyword. It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword. It is also used by class constructors to invoke constructors of its parent class.


implicit argument passing of super from method defined by , implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments handover all arguments for method build_dsl while calling super #108. Merged. If your method implementation needs to call the method of the same name as defined in the superclass, you can use the syntax ##super(), which is discussed in the subsections. This discussion applies to code that is written in ObjectScript.