Reputation: 519
I am currently trying to refactor some duplicate code. There is one statement in particular which is giving me the most trouble:
some_class = double("some_class", some_method?: true)
I need to replace some_method with a variable name. However, the below won't work. Below is just an example of what I am trying to do.
some_method = unique_method
some_class = double("some_class", some_method?: true)
Does any know of alternative way to write:
some_method? # where some_method is a var.
I need to be able to call
some_class.some_method?
for my test case.
Any help is much appreciated. Thank you in advance.
Upvotes: 2
Views: 174
Reputation: 11323
Surround that with a string, it should be fine, alternatively you could use the :some_method? => value
syntax.
def some_method?
puts "I am some_method? True"
end
some_hash_key = {:some_method? => true}
send(:some_method?)
my_variable = :some_method?
send(my_variable)
p some_hash_key
Upvotes: 1
Reputation: 519
OK.. so at the end of the day, the above worked.. here is a working example of what I was trying to do.. I am using a lambda to simulate the function
some_method_one = lambda { return }
some_method_one = lambda { return }
picked_method = some_method_one
some_class = double("some_class", picked_one?: true )
some_class.picked_one? # this returns true
This code is completely over simplified. What I am intending to do (haven't finished writing the logic yet) is to create a shared_examples_for in rspec. I have 4 methods that will use the same logic for testing, so I need to pass in the method as a variable. Hopefully this makes sense, and helps someone out.. :-)
Upvotes: 1