Rails

How To Call An Objects Private Methods In Ruby

Posted by Weston Ganger on March 05, 2016

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:


Posted in Rails and Tagged with rails ruby 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts