Questions tagged [instance-eval]

38 questions
2
votes
3 answers

define_method inside instance_eval

When I define a method inside instance_eval block for class it creates a class method which is fine. Eg) class A end A.instance_eval do def method; end end A.method #works But when I use define_method inside instance_eval it creates instance…
Rohan Pujari
  • 788
  • 9
  • 21
2
votes
2 answers

`instance_eval` and scopes

I have the following code: class A def self.scope yield end def self.method_added method self.instance_eval %{ # do something involving the added method } end end class B < A scope do def foo end …
Thermatix
  • 2,757
  • 21
  • 51
1
vote
1 answer

Where does Ruby store method defined in instance_eval()

Here is an example: class MyClass end obj = MyClass.new obj.instance_eval do def hello "hello" end end obj.hello # => "hello" obj.methods.grep "hello" # => ["hello"] MyClass.instance_methods.grep "hello" # => [] MyClass's instance…
Rambo
  • 1,834
  • 2
  • 13
  • 10
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

Ruby instance_exec / instance_eval with arguments

I'm trying to dynamically call a method given in a string using parameters given in the same string, I'm getting stuck on supplying the parameters though... I currently have: query = Query.new while true input = gets.split(%r{[/[[:blank:]]/,]}) …
Ozymandias
  • 521
  • 2
  • 16
1
vote
4 answers

Passing around procs between different objects

I'm trying to make the following work but I'm obviously missing something: class Person def fetch puts 'Fetch it boy!' end def action(data) data.call end end class Animal def play Person.new.action(proc { fetch }) …
stratis
  • 7,750
  • 13
  • 53
  • 94
1
vote
1 answer

Re-using Ruby DSL in a REPL or irb?

I have developed a simple DSL for tasks on a UniVerse database in jruby. It looks something like this support = { :host => 'localhost', :account => 'SUPPORT' } uni_task support do connect exec "LIST FILE A1" …
1
vote
2 answers

Ruby DSL - invoking user-provided methods within block context

A user of my Ruby DSL wishes to be able to add additional methods and invoke them within the context of an arbitrary block, for example: def do_something override 'flags', 'ABC' end project('XXX') do configuration('debug') do …
user1282993
1
vote
2 answers

How do I refactor code that uses `instance_exec` several times?

I'm working on a class that generates a PDF using Prawn gem. I have some similar methods. All of them start with the same line. Here is the code: module PDFGenerator class MatchTeamInfo include Prawn::View def initialize(match) …
1
vote
4 answers

Shortest code for getting all K-item combinations of N-item array, where K <= N

What is the shortest way of getting all K-item combinations of an N-item array where K <= N? I managed to write down the one below : > [1,2,3].instance_eval "(1..size).flat_map {|i| self.combination(i).to_a }" => [[1], [2], [3], [1, 2], [1, 3],…
boggy
  • 192
  • 2
  • 4
1
vote
1 answer

instance_eval's behaviour inside a instance method

Below is my snippet which i tried, class Person def test(arg) self.class.instance_eval do define_method(arg) {puts "this is a class method"} end end end irb(main):005:0> Person.new.test("foo") =>…
Kranthi
  • 1,377
  • 1
  • 17
  • 34
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
2 answers

How to pass a method to instance_eval?

I want to call instance_eval on this class: class A attr_reader :att end passing this method b: class B def b(*args) att end end but this is happening: a = A.new bb = B.new a.instance_eval(&bb.method(:b)) # NameError: undefined local…
michelpm
  • 1,795
  • 2
  • 18
  • 31
1
vote
2 answers

ruby metaprogramming - yield block not working in dynamically added method

I'm working on extending the NotAMock framework for stubbing methods in rspec, and getting the stubs to yield to a methods block. The code in this Gist works perfectly when I code it on my own (which is done-up to resemble how NotAMock stubs…
Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
1
vote
1 answer

Why does the second 'p arg' report the Foo instance?

class Foo def with_yield yield(self) end def with_instance_eval(&block) instance_eval(&block) end end f = Foo.new f.with_yield do |arg| p self # => main p arg # => # end f.with_instance_eval do |arg| p…
uzo
  • 2,821
  • 2
  • 25
  • 26