[RUBY] [Rails] What is a dot (.) Or a colon (:)?

I often see :: and .. This is a clarification that the notation changes due to the difference in method call.

Dot notation

python


class User
  def method
    puts "Hello"
  end
end

python


> user = User.new

> user.method
Hello
=> nil

Colon notation

python


class User
  def method
    puts "Welcome"
  end
end

python


> user = User.new
> user::method
Welcome
=> nil

Difference between dot notation and colon notation

The difference between dot notation and colon notation is whether you can call a constant.

notation Method call Calling a constant
Dot notation ×
Colon notation

In Ruby, classes and modules are also treated as constants.

python


require 'uri'
require 'net/http'

url = URI.parse("http://yahoo.co.jp")
http = Net::HTTP.start(url.host, url.port)

python


> document = http.get(url.path)

> puts document.body
#View document

Summary

In this way, the colon notation allows you to access items in a class or module in another specified file.

Recommended Posts

[Rails] What is a dot (.) Or a colon (:)?
What is a constructor?
What is a stream
What is a Servlet?
What is a wrapper class?
What is a boolean type?
What is a Ruby module?
What is a floating point?
What is Rails gem devise?
What is a meaningful comment?
What is a Java collection?
What is a fa⁉ enum?
What is Rails Active Record?
[Ruby on Rails] What is Bcrypt?
What is a snippet in programming?
What is a reference type variable?
What is a lambda expression (Java)
[Swift] What is "inheriting a class"?
What is a Ruby 2D array?
What is a class in Java language (3 /?)
What is a terminal? -Absolute path & relative path-
What is a Spring Boot .original file?
What is follow_redirect! Following ?: Rails Tutorial Memorandum-Chapter 7
[For programming beginners] What is a method?
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is a request scope? Image commentary
What is Cubby
What is Docker?
What is java
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
After all, what is [rails db: migrate] doing?
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is a safety reference operator (&.) Using Ruby ampersand?
What is the difference between a class and a struct? ?? ??
Understanding ruby's "|| =" specification and what is Rails presence method?
[Rails] What is the difference between redirect and render?
What is Maven Assembly?
What is `docker-compose up`?
What is vue cli
What is an interface?
What is Ruby's self?