# Ruby and class constants

**URL:** <https://www.ruby-forum.com/t/ruby-and-class-constants/260910>\
**Category:** Development\
**Created:** [February 2, 2021, 6:07pm UTC](https://www.ruby-forum.com/t/ruby-and-class-constants/260910 "2021-02-02T18:07:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luciotbc](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/luciotbc/32/2187_2.png) [@luciotbc](https://www.ruby-forum.com/u/luciotbc)\
**Post date:** [February 2, 2021, 6:07pm UTC](https://www.ruby-forum.com/t/ruby-and-class-constants/260910/1 "2021-02-02T18:07:40Z")

</div>

I have a confusion to access class constants, for example:

```
class GoogleApi
  API_ENDPOINT = 'https://www.google.com/'
end

```

I can’t understand what’s the difference between **GoogleApi.API\_ENDPOINT** and **GoogleApi::API\_ENDPOINT**?

Furthermore why sometimes when I try to access the constant using a _GoogleApi.API\_ENDPOINT_ and it raises a **NoMethodError: undefined method** error?

Thanks,  
Lucio

---

<div class="post-metadata">

**Author:** ![pcl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecae2f/32.png) [@pcl](https://www.ruby-forum.com/u/pcl)\
**Post date:** [February 2, 2021, 6:16pm UTC](https://www.ruby-forum.com/t/ruby-and-class-constants/260910/2 "2021-02-02T18:16:17Z")

</div>

:: is used to access the constant - the GoogleApi classname is the ‘namespace’ for the constant.

. is used to access a class method (so will not work with your API\_ENDPOINT)

```auto
class A
  B = 3

  def self.C
    4
  end
end

> A::B # gets the constant value
3
> A.C # calls the method named C
4

```

You get an error with either `A.B`, as B is not a method, or `A::C`, as C is not a constant.

(The same works with ‘module’ in place of ‘class’.)

---

<div class="post-metadata">

**Author:** ![ronnu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9fc348/32.png) [@ronnu3](https://www.ruby-forum.com/u/ronnu3)\
**Post date:** [March 26, 2021, 7:47pm UTC](https://www.ruby-forum.com/t/ruby-and-class-constants/260910/3 "2021-03-26T19:47:14Z")

</div>

thanks for the answer, I also had this problem

---

<div class="post-metadata">

**Author:** ![bleak\_gravel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bleak\_gravel](https://www.ruby-forum.com/u/bleak_gravel)\
**Post date:** [August 29, 2024, 12:52pm UTC](https://www.ruby-forum.com/t/ruby-and-class-constants/260910/4 "2024-08-29T12:52:29Z")

</div>

> [@pcl](#):
>
> :: is used to access the constant - the GoogleApi classname is the ‘namespace’ for the constant.
> 
> . is used to access a class method (so will not work with your API\_ENDPOINT)
> 
> ```auto
> 
> ```

This applies to the precise task, described by **luciotbc** , in the OP, above.

However, you must understand that the point ‘.’ is referring quite generally to an **object** , the two colons ‘::’ to a **class**.

Example

> puts "file size is " \<\< File::size(‘/tmp’).to\_s  
> puts "file size is " \<\< File.new(‘/tmp’).size.to\_s

produce identical output, while two different methods are employed. The File-class has 1 class-method size() which belongs to an object of class ‘_Class_’ of the name of File. The File class has also 1 Instance-method size() belonging to each object of class ‘_File_’.  
You can write the second line as

> fl = File.new(‘/tmp’)  
> puts "file size is " \<\< fl.size.to\_s

As `File::size()` is obviously a call to a class-method, you can also write it as `File.size` and have identical output (where _File_ is an object of type _class_). The double-colon is though clearer.
