liveBook · Manning

Public method

As explained in The Well-Grounded Rubyist, Fourth Edition

A non-private method is a Ruby method that can be invoked through ordinary method-call syntax with an explicit receiver.

A public method is a Ruby method that can be called through ordinary method-call syntax with an explicit receiver. Public is Ruby’s default access level, so a method remains public unless it is declared protected or private. Public instance methods form the everyday interface through which objects respond to messages. section 5.3

Calling and inspecting public methods

A string exposes public operations such as `upcase`, `downcase`, and `swapcase`; calling `string.upcase` sends the `upcase` message through an explicit receiver. To inspect available non-private methods, call `string.methods`, then filter and sort the returned array of symbols. section 5.3 section 15.2.1

>> string = "Test string"
=> "Test string"
>> string.methods.grep(/case/).sort
=> [:casecmp, :casecmp?, :downcase, :downcase!, :swapcase, :swapcase!,
➥ :upcase, :upcase!]

Inspect a string’s case-related method names, including public operations and their bang-method variants. section 15.2.1

Public methods should be distinguished from the broader result of `methods`: that query lists non-private methods, meaning public or protected methods. Method-name searches can reveal likely capabilities, but interpreting the results still requires judgment; naming conventions provide useful clues rather than a complete semantic explanation. section 15.2 section 15.2.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines default visibility and contrasts public access with private and protected methods, then stops at method-list inspection.
Examples
Uses string case methods, bang-method searches, and filtered arrays returned by `methods`.
What it assumes
Assumes familiarity with Ruby objects and message sending while reinforcing visibility distinctions from the default public level.
What you can do after
Apply it via ordinary receiver calls and inspect likely object capabilities with `methods`, filtering, and sorting.
How it connects
Connects public methods with objects, messages, private methods, protected methods, and runtime method introspection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.