Why Are Methods in Ruby Documentation Preceded by a Hash Sign

Why are methods in Ruby documentation preceded by a hash sign?

From the rdoc docs (emphasis mine):

Names of classes, source files, and
any method names containing an
underscore or preceded by a hash
character
are automatically
hyperlinked from comment text to their
description.

What is the # (sharp, number, pound, hash) sign used for in Ruby?

This is how instance method described:

Array#fill 

So you can:

a = Array.new(2)
=> [nil, nil]
a.fill(42)
=> [42, 42]

This is how class method described:

String::new

s = String.new('abc')
=> "abc"

Referring to javascript instance methods with a pound/hash sign

I think it comes from javadoc.

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javadoc.html#{@link}

What does class#method mean in ruby?

# = instance method

:: = class method

Per ruby docs:

Use :: for describing class methods, # for describing instance methods, and use . for example code.

Difference between . and #

The hash format (Class#method) is not valid ruby, but is used in documentation to describe an instance method.

Class methods are typically documented using a double-colon (Class::method).

You will see examples of both in the ruby docs (e.g. http://www.ruby-doc.org/core-1.9.3/String.html)

The dot format is used in code when actually calling a class method (Class.method), though I have seen some people (unfortunately) use it interchangeably with either the double-colon or hash in documentation.

Are these two Ruby methods with a hash parameter equivalent? If not, why not?

In the second, you set the param2 to an empty hash before giving it as a parameter to meth_x. In the method definition the param2 = {} means that if the parameter is omitted, then it is set to this default empty hash, but in the meth_x(param2 = {}) means that you drop the original content of param2 and replace it with an empty hash, then giving it to meth_x.

irb(main):001:0> a = {:alma => 2}
=> {:alma=>2}
irb(main):002:0> puts a
{:alma=>2}
=> nil
irb(main):003:0> puts(a)
{:alma=>2}
=> nil
irb(main):004:0> puts(a = {})
{}
=> nil

Need explanation of some Ruby syntax

  1. The colon character (:) is the beginning of a syntax literal for a Ruby "Symbol":

    :abc.class # => Symbol
    "abc".to_sym # => :abc

    Symbols are like strings but they are "interned", meaning the Ruby interpreter only has a single copy of it in memory despite multiple possible references (whereas there can be many equivalent strings in memory at once).

  2. The 'validates' token in your example above is a class method (of something in the class hierarchy of the "Post class") that is being called with a symbol argument (:name) and a hash argument with a single key/value pair of :presence => true.

  3. The 'create_table' token is a method which is being called with a single argument (the symbol ":posts") and is given a block which takes a single argument "t" (do |t| ... end).

What does the question mark at the end of a method name mean in Ruby?

It is a code style convention; it indicates that a method returns a boolean value (true or false) or an object to indicate a true value (or “truthy” value).

The question mark is a valid character at the end of a method name.

https://docs.ruby-lang.org/en/2.0.0/syntax/methods_rdoc.html#label-Method+Names



Related Topics



Leave a reply



Submit