How To Call An Objects Private Methods In Ruby

Posted by Weston Ganger

Sometimes you need to run an objects private method from the console or something.

For example your model may look something like this:


class Post < ActiveRecord::Base
  #...

private

  def set_initial_values
    # do work
  end

  def update_title(new_url = false)
    # do work
  end
end

Fortunately it can be done using the send method


@post.send(:set_initial_values)

# or with arguments

@post.send(:update_title, true)


Related External Links:

Article Topic:Software Development - Rails

Date:March 05, 2016

Recommended Posts