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

Contact

604-309-8713
info@solidfoundationwebdev.com

Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected