Questions tagged [instance-eval]
38 questions
0
votes
2 answers
instance_eval doesn't work with att_accessor?
Can anybody say me, why that isn't working:
class A
attr_accessor :b
end
a = A.new
a.instance_eval do
b = 2
end
a.b
=> nil
What is wrong i'm doing?

freeze
- 546
- 6
- 16
0
votes
2 answers
How to temporarily redefine a method in Ruby?
Say, I have:
class Test
def initialize(m)
@m = m
end
def test
@m
end
end
How can I temporarily make method #test of all instances (both existing and new ones) of Test return 113, and then restore the original method later?
It…

Bo Yi
- 123
- 1
- 9
0
votes
2 answers
Why use 'instance_eval' instead of creating a method?
Here's a code sample:
class Book
def initialize
@v = "abc"
end
end
b = Book.new
b.instance_eval do
def book_name
end
end
Why do we use instance_eval to create a method (book_name) instead of adding book_name method inside class Book?…

rohan kharvi
- 118
- 10
0
votes
1 answer
Instance_eval doesn't have access to class in module
I've came across an issue with instance_eval and module inclusion.
Please take a look at the following code:
module B
class C
def initialize
puts 'This is C'
end
end
def hello
puts 'hello'
end
end
class A
include B
…

José P. Airosa
- 368
- 1
- 2
- 11
0
votes
3 answers
Ruby: Properly using Lambdas
Before I start, I have tried fiddling with instance_eval and singleton methods to no avail. I am going to present my "best" attempt at the problem.
I am trying to do the following:
value = rule(condition: lambda {@something >…

Daiwik Daarun
- 3,804
- 7
- 33
- 60
0
votes
1 answer
Why does instance_eval handle direct accessors in a special way?
Why does the following code not raise an error:
Object.new.instance_eval { some_accessor_that_does_not_exist= "a value" }
While the following would raise a NameError as you would expect:
Object.new.instance_eval { some_method_that_doesnt_exist…

Redbeard
- 988
- 5
- 8
0
votes
0 answers
Defining attr_accessor for class instance variables - Ruby
I am trying to create an accessor for a class instance variable. I am calling the attr_accessor method from a module which is included in the class. See the code below:
module Persistence
def self.included(mod)
mod.extend ClassMethods
…

ShadyKiller
- 700
- 8
- 16
0
votes
1 answer
Instance_eval block not supplied?
Does anybody know what's causing this error? I'm trying to make a basic rack application.
App.rb =>
class Cherry
class << self
def app &block
Cherry::Application.new &block
end
end
class Application
…

andy
- 2,369
- 2
- 31
- 50