E
E
eldar_web2018-06-27 11:51:33
Ruby on Rails
eldar_web, 2018-06-27 11:51:33

What to do in Ruby (Rails) if a class method is not visible inside some block?

Let's say we have a class:

class MyClass

def a_method
 ...
end

def b_method
 Technology.new do |t|
   a_method()
 end
end

end

Here in method B in the Technology block, method A is not visible.
How can I solve the problem?
Making method A a class method is not an option.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Blokhin, 2018-06-27
@TITnet

codepad.org/HcmmPAgg

# Technology
class Technology
  attr_accessor :val

  def initialize
    @val = 42
    yield self
  end
end

# MyClass2
class MyClass
  # @param [Integer] val
  # @return [NilClass]
  def a_method(val)
    puts val
  end

  # @return [NilClass]
  def b_method
    Technology.new do |technology|
      a_method technology.val
    end
  end
end

my_class = MyClass.new
my_class.b_method # => 42

Everything should work.
Show the error that the interpreter throws.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question