Questions tagged [class-eval]
41 questions
22
votes
3 answers
Ruby - Using class_eval to define methods
I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment
I'm having a really hard time grasping this concept, this is what I've attempted to do:
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
…

8vius
- 5,786
- 14
- 74
- 136
16
votes
1 answer
What's the variable scope within `class_eval` string?
I am using class_eval to write code to be executed under the context of current class. In the following code, I want to add a counter for changes of attribute values.
class Class
def attr_count(attr_name)
attr_name = attr_name.to_s
…

steveyang
- 9,178
- 8
- 54
- 80
8
votes
2 answers
class_eval vs instance_eval
Is there any difference in how class_eval & instance_eval work except def? Inside class_eval block def defines method to class itself (i.e. instance method) and inside instance_eval def defines method to the eigenclass of the class (i.e. class…

Alexey
- 9,197
- 5
- 64
- 76
7
votes
4 answers
Ruby instance_eval on a class with attr_accessor
I understand the basic difference between instance_eval and class_eval. What I've discovered though when playing around is something strange involving attr_accessor. Here's an example:
A = Class.new
A.class_eval{ attr_accessor :x }
a = A.new
a.x =…

brad
- 31,987
- 28
- 102
- 155
5
votes
2 answers
Is it possible to define class methods within `class_eval`?
I know it's possible to define instance methods using class_eval. Is it possible to define class methods within the context of class_eval?

Andrew Grimm
- 78,473
- 57
- 200
- 338
3
votes
2 answers
Whats the difference between class_eval and class << className?
I am a Ruby starter. I found both of these are quite similar (in output), but i couldn't understand the difference in the below context. For example, I have a class
class Say
def self.hello
puts "hello"
end
end
and can be extended like…

RameshVel
- 64,778
- 30
- 169
- 213
3
votes
1 answer
Why is class_eval sometimes used when re-opening the class could work?
I've come across some code in a Rails app in the form of
ThirdPartyLibrary::Foo.class_eval do
def bar?
@bar_field == 'true'
end
end
and I'm wondering why they didn't just do
class ThirdPartyLibrary::Foo
def bar?
@bar_field == 'true'
…

Andrew Grimm
- 78,473
- 57
- 200
- 338
3
votes
1 answer
How does module_eval / class_eval / instance_eval counts the line numbers
I have found the line_number passed to class_eval, module_eval and instance_eval doesn't match the line numbers reported by the error. This behaviour is not explained by the ruby-doc which says: (use instance_eval as an example)
the optional…

steveyang
- 9,178
- 8
- 54
- 80
3
votes
1 answer
Creating methods with class_eval
I want to write a method which takes one parameter and creates another method, named with this parameter. Here is my code
class Class
def createMethod(attr_name)
attr_name = attr_name.to_s
class_eval %Q{
def #{attr_name}
…

ciembor
- 7,189
- 13
- 59
- 100
2
votes
1 answer
ruby method_alias in inherited class
I'm deeping into ruby metaprogramming and have next question.
Example:
module ExampleAliaser
def do_example_alias(prefix=:origin)
class_eval <<-EOS
class << self
alias_method :#{prefix}_example, :example
def…

Fivell
- 11,829
- 3
- 61
- 99
2
votes
1 answer
Ruby: How to eager load class contents before a module gets loaded in its parent class
I've got a few classes with a constant SCHEMA
class Consumable::ProbeDesign < Consumable
SCHEMA = {
"type": "object",
"properties": { },
"required": []
}
end
class DataModule::WaterDeprivationLog < DataModule
SCHEMA = {
…

Andrew
- 942
- 10
- 26
2
votes
1 answer
Override Controller in Spree - Circular dependency detected while autoloading constant
I'm trying to add action to Spree Admin PromotionsController like this
app/controllers/spree/admin/promotions_controller.rb
Spree::Admin::PromotionsController.class_eval do
def users
params[:q] ||= {}
@search =…

Serhii Budnik
- 23
- 1
- 5
2
votes
1 answer
Deep into Ruby class_eval and instance_eval
class_eval and instance_eval are quite predictable in such cases like defining methods. I also understand the difference between class's instance and class's singleton (aka eigenclass).
BUT
I cannot figure out the only thing like following:
Let's…

bernstein7
- 111
- 5
2
votes
1 answer
How to remove OAUTH_PROVIDERS in Spree Social plugin
I've been reading through the SpreeSocial documentation here. I can't figure out how to remove providers through the config. I've tried to just pop the most recent provider off off of the list but that's not working.
…

Mary
- 109
- 10
2
votes
1 answer
Unable to decipher this Ruby line containing map operator
I've just seen this line of Ruby code in ruby-trello:
# Returns the member who created the action.
one :member_creator, :via => Member, :using => :member_creator_id
It seems to relate to a superclass method defined as:
def self.one(name, opts =…

KomodoDave
- 7,239
- 10
- 60
- 92