So, I want to do a list of methods and after use that list in for-loop.
class Hello
def func
pass
end
def func1
pass
end
list = [func, func1]
def loop_func
for func_instance in list
func_instance
end
end
end
But this code does not work. What is wrong?
class Hello
def foo
p "Foo"
end
def bar
p "Bar"
end
def loop_methods
self.class.instance_methods(false)
.each{ |m| m == __method__ || self.send(m) }
end
end
Hello.new.loop_methods
Related
I have something similar to this:
Module A
Class A
def initialize
end
def m1
end
def m2
end
def m3
end
end
end
What I want is to run a validation before m1, m2, or m3 is executed, like a before_action, to return nil if is a condition is satisfied, for instance, if a variable is nil, return nil immediately.
I know that I can create a module:
module Callbacks
def callbacks
#callbacks ||= Hash.new { |hash, key| hash[key] = [] }
end
def before_run(method_name, callback)
callbacks[method_name] << callback
end
module InstanceMethods
def run_callbacks_for(method_name)
self.class.callbacks[method_name].to_a.each do |callback|
send(callback)
end
end
end
end
And inside the Class A, I can call:
before_run :m1, :my_validation_method
before_run :m2, :my_validation_method
before_run :m3, :my_validation_method
Is there any other clean way of doing this?
I don't sure, but I guess you can use prepend
For example:
module Baz
def foo
return nil if #a == 0
super
end
end
class Foo
prepend Baz
def initialize(a)
#a = a
end
def foo
p 'foo'
end
end
User like
Foo.new(0).foo
=> nil
Foo.new(1).foo
"foo"
=> "foo"
How can a parent get the constructor arguments of a child ?
class A
include Parent
def initialize(foo, bar)
#foo = foo
#bar = bar
end
end
class B
include Parent
def initialize(foo)
#foo = foo
end
end
module Parent
def print_args
# here is the code for print args of child, this is not real code
puts Child.args # this is not real code
end
end
The expected behavior would be :
a = A.new('hello', 'world')
a.print_args
=> "hello world"
b = B.new('hello')
b.print_args
=> "hello"
The Parent module should not now the args names
One way is to have the "children" implement a method that returns their arguments:
class A
include Parent
def initialize(foo, bar)
#foo = foo
#bar = bar
end
def args
[#foo, #bar]
end
end
class B
include Parent
def initialize(foo)
#foo = foo
end
def args
[#foo]
end
end
The "parent" can than call that method without having to know its implementation:
module Parent
def print_args
puts args.join(' ')
end
end
If your module is included in many classes and you want to display instance variable values space separated, then you can do as follow,
using only ruby,
def print_args
instance_variables.map { |x| instance_variable_get(x) }.join(' ')
end
using rails,
def print_args
instance_values.values.join(' ')
end
You're asking how to get the "constructor arguments from the parent" and since almost everything is possible in Ruby: if you're really adventurous (read: don't do this), you can override the new method upon including Parent in order to intercept its arguments and define a singleton method on the instance which prints the argument:
module Parent
def self.included(mod)
def mod.new(*args)
super.tap do |instance|
instance.define_singleton_method(:print_args) do
puts args.join(' ')
end
end
end
end
end
Example usage:
class A
include Parent
def initialize(foo, bar)
end
end
A.new('hello', 'world').print_args
# prints "hello world"
The instance doesn't even have to store the arguments in instance variables.
I have several methods which have similar conditional logic and I think it's reasonable to DRY them.
class A
def parent1(val)
puts "parent1 A #{val}"
end
def parent2(val)
puts "parent2 A #{val}"
end
end
class B < A
def parent1(val)
if val
puts "foo"
else
super
end
end
def parent2(val)
if val
puts "bar"
else
super
end
end
end
Is it possible to create a #puts_or_super which would be able to call super of its caller? (B#parent1 or B#parent2) So the code will look like this:
def parent1(val)
puts_or_super(val, "foo")
end
def parent2(val)
puts_or_super(val, "bar")
end
Edit: this works but looks insane
def puts_or_super(val, text)
if val
puts text
else
self.class.superclass.instance_method(caller[0][/`.*'/][1..-2].to_sym).bind(self).call
end
end
Any better solutions?
Suppose your definitions are in class A, and the relevant super methods are defined in class B so that A inherits from B.
I think you should go the other way around. Make B a module, and prepend B to A. Then, the definitions where you have the "foo", "bar", etc. would not be the super methods, and the conditions will be in the prepended module.
module B
def parent1(val)
return super if val
... # your original logic in super class
end
def parent2(val)
return super if val
... # your original logic in super class
end
end
class A
prepend B
def parent1(_); puts "foo" end
def parent2(_); puts "bar" end
end
Just for fun, again, but is it possible to take a block that contains method definitions and add those to an object, somehow? The following doesn't work (I never expected it to), but just so you get the idea of what I'm playing around with.
I do know that I can reopen a class with class << existing_object and add methods that way, but is there a way for code to pass that information in a block?
I guess I'm trying to borrow a little Java thinking here.
def new(cls)
obj = cls.new
class << obj
yield
end
obj
end
class Cat
def meow
puts "Meow"
end
end
cat = new(Cat) {
def purr
puts "Prrrr..."
end
}
cat.meow
# => Meow
# Not working
cat.purr
# => Prrrr...
EDIT | Here's the working version of the above, based on edgerunner's answer:
def new(cls, &block)
obj = cls.new
obj.instance_eval(&block)
obj
end
class Cat
def meow
puts "Meow"
end
end
cat = new(Cat) {
def purr
puts "Prrrr..."
end
}
cat.meow
# => Meow
cat.purr
# => Prrrr...
You can use class_eval(also aliased as module_eval) or instance_eval to evaluate a block in the context of a class/module or an object instance respectively.
class Cat
def meow
puts "Meow"
end
end
Cat.module_eval do
def purr
puts "Purr"
end
end
kitty = Cat.new
kitty.meow #=> Meow
kitty.purr #=> Purr
kitty.instance_eval do
def purr
puts "Purrrrrrrrrr!"
end
end
kitty.purr #=> Purrrrrrrrrr!
Yes
I suspect you thought of this and were looking for some other way, but just in case...
class A
def initialize
yield self
end
end
o = A.new do |o|
class << o
def purr
puts 'purr...'
end
end
end
o.purr
=> purr...
For the record, this isn't the usual way to dynamically add a method. Typically, a dynamic method starts life as a block itself, see, for example, *Module#define_method*.
(Big edit, I got part of the way there…)
I've been hacking away and I've come up with this as a way to specify things that need to be done before attributes are read:
class Class
def attr_reader(*params)
if block_given?
params.each do |sym|
define_method(sym) do
yield
self.instance_variable_get("##{sym}")
end
end
else
params.each do |sym|
attr sym
end
end
end
end
class Test
attr_reader :normal
attr_reader(:jp,:nope) { changethings if #nope.nil? }
def initialize
#normal = "Normal"
#jp = "JP"
#done = false
end
def changethings
p "doing"
#jp = "Haha!"
#nope = "poop"
end
end
j = Test.new
p j.normal
p j.jp
But changethings isn't being recognised as a method — anyone got any ideas?
You need to evaluate the block in the context of the instance. yield by default will evaluate it in its native context.
class Class
def attr_reader(*params, &blk)
if block_given?
params.each do |sym|
define_method(sym) do
self.instance_eval(&blk)
self.instance_variable_get("##{sym}")
end
end
else
params.each do |sym|
attr sym
end
end
end
end
Here's another alternative approach you can look at. It's not as elegant as what you're trying to do using define_method but it's maybe worth looking at.
Add a new method lazy_attr_reader to Class
class Class
def lazy_attr_reader(*vars)
options = vars.last.is_a?(::Hash) ? vars.pop : {}
# get the name of the method that will populate the attribute from options
# default to 'get_things'
init_method = options[:via] || 'get_things'
vars.each do |var|
class_eval("def #{var}; #{init_method} if !defined? ##{var}; ##{var}; end")
end
end
end
Then use it like this:
class Test
lazy_attr_reader :name, :via => "name_loader"
def name_loader
#name = "Bob"
end
end
In action:
irb(main):145:0> t = Test.new
=> #<Test:0x2d6291c>
irb(main):146:0> t.name
=> "Bob"
IMHO changing the context of the block is pretty counter-intuitive, from a perspective of someone who would use such attr_reader on steroids.
Perhaps you should consider plain ol' "specify method name using optional arguments" approach:
def lazy_attr_reader(*args, params)
args.each do |e|
define_method(e) do
send(params[:init]) if params[:init] && !instance_variable_get("##{e}")
instance_variable_get("##{e}")
end
end
end
class Foo
lazy_attr_reader :foo, :bar, :init => :load
def load
#foo = 'foo'
#bar = 'bar'
end
end
f = Foo.new
puts f.bar
#=> bar