Questions tagged [class-eval]

41 questions
2
votes
1 answer

Ruby metaprogramming: adding @variables to existing 'initialize' method (using class_eval)

Possible Duplicate: Ruby.Metaprogramming. class_eval I have this little project, the goal is to create a 'attr_accessor_with_history' method, that will keep a record of every single value assigned to variables created by it. Here's the code…
user1608920
  • 157
  • 2
  • 9
1
vote
1 answer

Extending classes in Ruby (def versus class_eval)

It seems like class_eval (takes a string that it turns into code) and declaring the class with new def methods are essentially the same thing. Are they really equivalent? What are the pros and cons of each?
DFBerry
  • 1,818
  • 1
  • 19
  • 37
1
vote
1 answer

How to make the array initialize only the first time function is called?

class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s # make sure it's a string …
user1065734
1
vote
1 answer

Ruby/Rails: class_eval doesn't want to evaluate this code

To generate mocks for Omniauth, I Added this method to config/environments/development.rb def provides_mocks_for(*providers) providers.each do |provider| class_eval %Q{ OmniAuth.config.add_mock(provider, { :uid =>…
Laura
  • 4,199
  • 6
  • 23
  • 24
1
vote
1 answer

Why is this class which inherits from ActiveRecord::Base incapable of class_eval ing scope

Note: I know what I'm doing here is probably stupid but I'm not even necessarily going to use this code I'm just curious at this point. I'm trying to dynamically add an empty Rails scope to a class in the event a specific method is missing on the…
Andrew
  • 942
  • 10
  • 26
1
vote
2 answers

How do you temporarily "class_eval" for a test?

Is it possible to temporarily apply certain methods to a class for tests? I want to be able to run specs depending on many ways to apply it. While I could make a bunch of fixtures with different settings, I find it easier to just class_eval the…
Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
1
vote
1 answer

Trying to understand the usage of class_eval

I'm using the rails-settings gem, and I'm trying to understand how you add functions to ActiveRecord classes (I'm building my own library for card games), and I noticed that this gem uses one of the Meta-programming techniques to add the function to…
kalbasit
  • 766
  • 8
  • 17
1
vote
1 answer

How to use a hash in a class_eval statement in Ruby

I was working on a homework assignment when I ran into a frustrating issue. The assignment is an exercise in Ruby metaprogramming and the goal is to define an 'attr_accessor_with_history' that does all the same things as 'attr_accessor', but also…
Xcelleratr
  • 85
  • 8
1
vote
0 answers

A better way to call methods on an instance

My question has a couple layers to it so please bear with me? I built a module that adds workflows from the Workflow gem to an instance, when you call a method on that instance. It has to be able to receive the description as a Hash or some basic…
Adam
  • 344
  • 1
  • 15
1
vote
0 answers

instance_eval with custom definee context

Im trying to create DSL customization for already existing lib and i have some misunderstanding about Ruby block contexts. Suppose we have a block saved as proc some_block = Proc.new do def testing; end puts self.inspect if self.kind_of?…
Bombazook
  • 495
  • 4
  • 14
1
vote
1 answer

undefined method `association_class' for nil:NilClass

I'm working with Spree 2.1 and trying to add new payment gateway, but this error is more generic so Spree itself is not-so-important here. I have encountered that error (undefined method 'association_class' for nil:NilClass) after adding some module…
zrl3dx
  • 7,699
  • 3
  • 25
  • 35
1
vote
1 answer

Ruby issue with class_eval

I'm trying to run the following but i keep getting a "NoMethodError: undefined method `bar=' for #/Foo:0x86f69ba/" class Class def my_attr_accessor(name) attr_name = name.to_s attr_reader name attr_reader "my_"+name …
Klam
  • 161
  • 1
  • 15
1
vote
1 answer

Regex error inside class-eval block

I've got some code to add methods to a module from simple definitions for talking to remote resources via a wrapper class around a REST client. def service_function(function_name, method, uri, parameters) class_eval <<-RUBY def…
0
votes
1 answer

class_eval issue Ruby

I am trying to use class_eval to create a method metaprogrammatically if the method that's called starts with "plus". However, I'm having trouble putting together the actual syntax of the class_eval class Adder def initialize(my_num) @my_num…
user1043856
  • 451
  • 1
  • 5
  • 7
0
votes
2 answers

Ruby 1.9.2 class_eval variables

In Settingslogic fork allowing array as source, in ruby 1.8.7 everything is working, but in ruby 1.9.2 there is an error. The problem is within this part of the code: self.class.class_eval <<-EndEval def #{key} return @#{key} if @#{key} …
santuxus
  • 3,662
  • 1
  • 29
  • 35