I'm trying to create a super class that will be extended. The super class will call a method that has to be implemented by the child class. The thing is, that method is called sometimes 3 blocks deep. In those blocks, I also refer to attributes of the class.
But, I get an error saying that there is no variable or method, and it's because the methods and variables are assumed to be from the block class.
This is how it looks like:
class SuperClass
attr_accessor :model
def initialize(model)
#model = model
end
def resources
s = Tire.search(get_index) do
query do
boolean do
must { term :model_id, model.id } #attr_accessor fails
must { all }
search_scope(self) #search_scope fails
end
end
sort do
sort_scope(self) #sort_scope fails
end
end
s.results
end
end
class SubClass < SuperClass
attr_accessor :params
def initialize(model, params)
#params = params
super(model)
end
def search_scope(boolean_query)
boolean_query.must { term field: params[:feild] }
#...
end
def sort_scope(sort_query)
sort_query.by :field, params[:sort_dir]
#...
end
end
search = SubClass.new(model, {})
results = search.resources # undefined method error as explained below
What I'm trying to achieve is calling the method search_scope and sort_scope (Implemented in child classes) that will set also set a few search and sort parameters. But I get undefined method 'search_scope' for #<Tire::Search::BooleanQuery:0x00000004fc9820>. As you can see, it's trying to call search_scope on the class of the block context. Same with the attr_accessor :model.
I know I can remedy this by doing
def resources
instance = self
# ...
end
And then calling instance.model and instance.search_scope, but this means my child classes have to define the instance in their own search_scope and sort_scope methods too.
I was wondering whether there is a better way to solving this?
Related
class Restaurant
attr_accessor :average_rating, :city
def initialize(city, name)
#city = city
#name = name
#number_of_ratings = 0
#sum_of_ratings = 0
end
def rate(new_rate)
#number_of_ratings += 1
#sum_of_ratings += new_rate
#average_rating = #sum_of_ratings.to_f / #number_of_ratings
end
def self.filter_by_city(restaurants, city)
restaurants.select { |restaurant| restaurant.city == city }
end
end
The above code is part of a challenge and I kept failing the tests for the #filter_by_city method. I checked the solution and the only difference was the self. prior to the method name. I've tried to understand what self does exactly but it's difficult to understand without context. In this particular class method, what exactly is self doing? I know what the body of the method is doing i.e. the filtering of the restaurants by city, but how does it run exactly?
self is the class Restaurant. def self.method is how you implement a method on the class itself rather than an instance of the class. Restaurant.filter_by_city(...) rather than Restaurant.new.filter_by_city(...).
self changes in Ruby depending on context. Within a method, self is the object the method was called on.
Within the class Restaurant block, and outside of any method, self is the Restaurant object which is a Class object. Everything is an object in Ruby. Everything.
You can also do this by declaring a block where the class is the instance.
class << self
def filter_by_city(restaurants, city)
restaurants.select { |restaurant| restaurant.city == city }
end
end
Normally you'd use this syntax if you have a lot of class methods.
See Self in Ruby: A Comprehensive Overview for more.
When defining a method in ruby you can optionally explicitly define that method's receiver using def <receiver>.<method> syntax instead of plain def <method>
object = Object.new
def object.foo
:foo
end
object.foo #=> foo
The receiver must either be a singular reference OR an expression (but it must be enclosed by brackets):
a = [Object.new, Object.new]
def (a.first).foo
:foo
end
def (a[1]).bar
:bar
end
a[0].foo #=> :foo
a.last.bar #=> :bar
a.first.bar #=> undefined method
When receiver is defined, the method is defined directly on the receiver's singleton class, ignoring the context in which the method is defined:
class A
o = Obejct.new
def o.foo
end
end
A.new.foo #=> undefined method
Even though method foo was defined in class A body, it is not available to its instances because of the explicit receiver
self is a ruby keyword returning the current "context". Inside the methods, self is (usually) a receiver of the call, and inside the module self returns that module. So:
module Wrapper
module SomeModule
puts self.name
end
end
will print Wrapper::SomeModule.
This means that:
class A
def self.foo
end
end
Is exactly the same as:
class A
def A.foo
end
end
So, the method is defined directly on A and can only be called directly on the class as A.foo, rather than on its instances.
I want to define methods dynamically using an array of strings.
Here is a simple piece of code that should achieve that.
class SomeClass
attr_accessor :my_array
def initialize(user, record)
#my_array=[]
end
my_array.each do |element|
alias_method "#{element}?".to_sym, :awesome_method
end
def awesome_method
puts 'awesome'
end
end
When I instantiate this class in the console, I get the following error
NoMethodError (undefined method `each' for nil:NilClass)
What is wrong with this code and how to make it work. any help highly appreciated :)
Edit 1:
What I ultimately want to achieve is to inherit from SomeClass and override my_array in the child class to dynamically define methods with its attributes like so
class OtherClass < SomeClass
my_array = %w[method1 method2 method3]
# Some mechanism to over write my_array.
end
And then use self.inherited to dynamically define methods in child class.
Is there a good way to achieve this?
In your code, you use an instance variable (#my_array) and an attr_accessor over it, and then try to access my_array from class level (that is, from the body of the class definition, outside of any methods). But instance variables only exist at instance level, so it is not available in the class scope.
One solution (the natural one, and the one which you would probably use in other languages) is to use a class variable: ##my_array. But class variables in ruby are a little problematic, so the best solution would be to make use of class instance variables, like that:
class SomeClass
class << self
attr_accessor :my_array
end
#my_array=[]
def initialize(user, record)
end
#my_array.each do |element|
alias_method "#{element}?".to_sym, :awesome_method
end
def awesome_method
puts 'awesome'
end
end
The syntax is a little tricky, so, if you look that up and it still doesn't makes sense, try just reading about scopes and using a regular class variable with ##.
Edit:
Ok, so, after your edit, it became more clear what you are trying to accomplish. A full working example is like follows:
class SomeClass
class << self
attr_accessor :my_array
end
#my_array=[]
def awesome_method
puts 'awesome'
end
def self.build!
#my_array.each do |element|
self.define_method("#{element}?".to_sym){ awesome_method }
end
end
end
class ChildClass < SomeClass
#my_array = %w[test little_test]
self.build!
end
child_instance = ChildClass.new
child_instance.test?
>> awesome
child_instance.little_test?
>> awesome
So, I've made some tweaks on SomeClass:
It does not need an initialize method
I tried to use the inherited hook for this problem. It won't ever work, because this hook is called as soon as "ChildClass < SomeClass" is written, and this must be before you can define something like #my_array = %w[test little_test]. So, I have added a self.build! method that must be called in the child instances so that they build their methods from my_array. This is inevitable, but I think it is also good, because it makes more explicit in the subclasses that you are doing something interesting there.
I think you want "define_method", not "alias_method".
awesome_method in passed in a block, which is ruby's way of doing functional programming.
With that done, ChildClass inherits from SomeClass, and it's instances have the dynamically created methods 'test?' and 'little_test?'.
You need to change my_array to class level accessible, in my case class constant.
class SomeClass
DYNAMIC_METHOD_NAMES = %w(method_a method_b method_C).freeze
def initialize(user, record)
end
DYNAMIC_METHOD_NAMES.each do |element|
alias_method "#{element}?".to_sym, :awesome_method
end
def awesome_method
puts 'awesome'
end
end
Question about a couple of methods that I saw in this tutorial: https://richonrails.com/articles/rails-presenters
In particular:
module ApplicationHelper
def present(model, presenter_class=nil)
klass = presenter_class || "#{model.class}Presenter".constantize
presenter = klass.new(model, self)
yield(presenter) if block_given?
end
end
And
class BasePresenter < SimpleDelegator
def initialize(model, view)
#model, #view = model, view
super(#model)
end
def h
#view
end
end
How does the present method work? I'm pretty confused about its arguments parameters, ie, model, presenter_class=nil along with the whole method.
And I'm also very confused about model, view arguments as well, and where/what is super(#model) method?
Any information that can explain those methods would be so immensely helpful because I've been staring at it for the past while wondering how the heck they work.
I'll give this a shot.
The present method accepts two parameters, a model, and a presenter class.
The presenter_class = nil means that presenter_class is an optional parameter, and will be set to nil if a variable is not passed as that parameter when the method is called.
I've added comments to the code to explain what's happening step by step.
# Define the Helper (Available in all Views)
module ApplicationHelper
# Define the present method, accepts two parameters, presnter_class is optional
def present(model, presenter_class=nil)
# Set the presenter class that was passed in OR attempt to set a class that has the name of ModelPresenter where Model is the class name of the model variable
klass = presenter_class || "#{model.class}Presenter".constantize
# ModelPresenter is initialized by passing the model, and the ApplicationHelper class
presenter = klass.new(model, self)
# yeild the presenter if the rails method block_given?
yield(presenter) if block_given?
end
end
Here's another question explaining how yield works
Here's some more info on the rails constantize method
The BasePresenter inherits from the SimpleDelegator class (documentation).
class BasePresenter < SimpleDelegator
def initialize(model, view)
# Set the instance variables #model and #view as the two parameters passed to BasePresenter.new
#model, #view = model, view
# calls the inherited SimpleDelegator initializer with the #model parameter
super(#model)
end
# An instance method that returns the #view variable that was set on initialization
def h
#view
end
end
I receive undefined method 'search_type' for the code below. Can you tell me what am I doing wrong here? Probably something with calling private functions, but I can't find what the problem is.
class Entry < ActiveRecord::Base
attr_accessible :content, :rank, :title, :url, :user_id
def self.search(params)
t, o = search_type(params[:type]),search_order(params[:order])
scope = self
scope = scope.where(t) if t
scope.order(o).page(params[:page]).per_page(20)
end
private
def search_order(order)
return 'comments_count DESC' if order == '1'
return 'points DESC' if order == '2'
'rank DESC'
end
def search_type(type)
return nil unless type.present?
"entry_type = #{type}"
end
end
In the controller, I have only #entries = Entry.search(params).
It's not to do with the privateness of your methods, but the fact that search is a class method, so when you call search_order from within it, it is looking for a class method called search_order but you've defined search_order as in instance method.
Make your 2 helper methods class methods and you should be ok. if you want them to be private class methods, then
class << self
def search(...)
end
private
def search_type(...)
end
def search_order(...)
end
end
If you are wondering why #entries.search(...) works it's because I assume that #entries is something like Entry.where(...) ie, a scope and you can call class methods on scopes.
search is defined as a class method, so you should call Entry.search(params) instead of #entries.search(params).
Your method is an a class method, you cant use it form instances of your class
Hey guys.
How do I know the methods that a child class overrided in my super class?
I have this:
class Test
def self.inherited(child)
# child.overrided_methods???
end
def self.foo
end
def self.bar
end
end
def Child < Test
def self.bar
puts "bar"
end
end
The method self.inherited is called when a subclass of Test is loaded. So I get the reference to this subclass in child, but I don't know how to get the methods that were overrided by this subclass.
Any ideas?
--
Arsen suggested the use of self.method_added(name) instead of self.inherited(child), but this method catches only instance methods and I want to catch class methods. Does anyone know another methods that does the same thing but with class methods?
In the last case I'll consider using a singleton and convert all this class methods to instance methods then the problem is solved.
For instance methods there is an Object::method_added(name) method you can override, similar to 'inherited' you have used:
class test
def self.method_added(name)
puts "method_added(#{name.inspect})"
super
end
end
irb(main):002:0> class Child < Test; def foo; end; end
method_added(:foo)
=> nil
You can then compare a received name to a list of your methods:
Test.instance_methods.include?(name.to_s)
With class methods this approach does not work (even if you do things like class << self magic), but a helpful fellow knew the answer: http://www.ruby-forum.com/topic/120416 :
class Test
def self.singleton_method_added(name)
puts "Class method added #{name.inspect}"
end
end
This is only the first part of the problem, because you need to know which class defined the method (it will be self) and whether the method is a new one, or overridden one. Experiment with this code:
class Test
def self.singleton_method_added(name)
if self == Test
puts "My own class method added: #{self.name}.#{name.inspect}"
elsif Test.methods(false).include?(name.to_s)
puts "Class method overriden: #{self.name}.#{name.inspect}"
elsif Test.methods(true).include?(name.to_s)
puts "My parent's class method overriden: #{self.name}.#{name.inspect}"
else
puts "New class method added: #{self.name}.#{name.inspect}"
end
end
end
Maybe a first step to the solution:
By calling child.instance_method(:bar) (if child refers to the class) or child.method(:bar) (if it refers to an instance of Child) you can get an UnboundMethod or Method object representing your method:
a = Test.instance_method(:foo)
b = Child.instance_method(:foo)
Unfortunately, a == b evaluates to false, although both refer to the same method.
def overridden_methods
klass = self.class
klass.instance_methods.select {|m| klass.instance_method(m).owner == klass}
end
Change according to your needs.