Codebeerstartups

Private Class Methods In Ruby

Introduction

user

Mohit Jain

Rails Developer


Private Class Methods, Ruby

Private Class Methods In Ruby

Posted by Mohit Jain on .
Featured

Private Class Methods, Ruby

Private Class Methods In Ruby

Posted by Mohit Jain on .

A quick tip to how to define a class method as a private method, I have seen a lot of people doing it the wrong way. Let’s take a quick look on how to define a private instance method in Ruby

class Dog
  def poop
    "Going outside, and will poop :)"
  end

  private

  def bark
    puts 'woof woof'
  end
end

In the above code, poop is a public method and bark is a private method. If you are calling a public method it will be something like:

> dog = Dog.new
> dog.poop
# => "Going outside, and will poop :)"

But calling a private method will give you an error.

> dog = Dog.new
> dog.bark
# NoMethodError: private method `bark' called for #<Dog>

Now define private class method is not as it is for instance method. They don’t exist as normally as instance methods are there but still, they exist.

if you want to define a class method private, let’s try it the way instance method works,

class Dog

  private

  def self.things_can_be_done
    [:bark, :poop, :sleep, :eat]
  end
end

> Dog.things_can_be_done
  # => [:bark, :poop, :sleep, :eat]

Oops, a private class method has been called :(. This is because the way Ruby defines the class methods, the self is actually Dog and the private method scope was never considered when Ruby was defining this method as a class method. Here are a couple of ways you can define a class method as private.

1. private_class_method

Quick way and easiest way to define a class method as private.

class Dog

  def self.things_can_be_done
    [:bark, :poop, :sleep, :eat]
  end

  private_class_method :things_can_be_done
end

> Dog.things_can_be_done
  # => NoMethodError: private method `things_can_be_done' called for Dog:Class

2. Using self as block

Block of class methods :)

class Dog
  class << self
    private

    def things_can_be_done
      [:bark, :poop, :sleep, :eat]
    end
  end
end

> Dog.things_can_be_done
  # => NoMethodError: private method `things_can_be_done' called for Dog:Class

Good luck ;)

user

Mohit Jain

http://codebeerstartups.com

Rails Developer