0

what does : mean in private :getWidth, :getHeight?

# define private accessor methods
   def getWidth
      @width
   end
   def getHeight
      @height
   end
   # make them private
   private :getWidth, :getHeight
lei lei
  • 1,753
  • 4
  • 19
  • 44
  • 1
    `:getWidth` and `:getHeight` are `Symbols` (btw by common Ruby conventions those methods should be named `get_width` and `get_height`). Does this answer help? https://stackoverflow.com/a/11447568/2483313 – spickermann Feb 24 '22 at 06:56
  • See the docs for [`private`](https://ruby-doc.org/core-3.1.1/Module.html#private-method) for details about the arguments it takes – Stefan Feb 24 '22 at 08:24
  • 1
    @spickermann `width` and `height` actually - prefixes should be avoided. – max Feb 24 '22 at 10:05

1 Answers1

2

The : sigil in Ruby denotes a symbol. Symbols are immutable strings which are used as identifiers all over the Ruby language.

private :getWidth, :getHeight

Sets the visibility of the getWidth and getHeight method to private. Its an alternative way of writing:

private

def getWidth
  @width
end

def getHeight
  @height
end

Beyond that this code is very unidiomatic. Getter methods in Ruby should not be prefixed and method names should always be snake_case and not camelCase.

private

def width
  @width
end

def height
  @height
end

Or:

private

attr_reader :height, :width
max
  • 96,212
  • 14
  • 104
  • 165