# Class methods invocation works unexpectedly

**URL:** <https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131>\
**Category:** Help & Support\
**Created:** [November 24, 2022, 5:04pm UTC](https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131 "2022-11-24T17:04:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ya55en](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ya55en/32/1819_2.png) [@ya55en](https://forum.crystal-lang.org/u/ya55en)\
**Post date:** [November 24, 2022, 5:04pm UTC](https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131/1 "2022-11-24T17:04:16Z")

</div>

Hi Crystal community,

Just discovered Crystal a week or so ago - fascinating thing!

I have this issue to sort out: calling class methods within the scope of the class works only with a fully qualified name `AClass.a_method`. I would expect (and I believe [the docs](https://crystal-lang.org/reference/1.6/syntax_and_semantics/class_methods.html) are saying) that within the scope of the unit (class or module) one can call static methods with `self.a_static_method` … no? What am I missing?

An example follows:

```crystal
class AClass
  def initialize(@name : String)
  end

  def this_fails
    # Error: undefined method 'class_method' for AClass
    self.class_method(@name)
  end

  def this_also_fails
    # Error: undefined method 'class_method' for AClass
    class_method(@name)
  end

  def this_works
    AClass.class_method(@name)
  end

  def self.class_method(name : String) : Nil
    puts "class_method called with name=#{name}"
  end
end

ac = AClass.new "Bimbo"
ac.this_works
ac.this_fails
ac.this_also_fails

```

Any help would be much appreciated, thank you!

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 24, 2022, 5:15pm UTC](https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131/2 "2022-11-24T17:15:40Z")

</div>

> [@ya55en](#):
>
> I would expect (and I believe [the docs](https://crystal-lang.org/reference/1.6/syntax_and_semantics/class_methods.html) are saying) that within the scope of the unit (class or module) one can call static methods with `self.a_static_method` … no? What am I missing?

`self` in the context of an instance method is referring to the instance itself, which is a different scope compared to the class scope the class methods are defined in. The idiomatic way would be to do like `self.class.class_method @name`, which is an easy way to get a reference to the current instance’s class scope.

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [November 24, 2022, 5:19pm UTC](https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131/3 "2022-11-24T17:19:37Z")

</div>

The docs could perhaps be clearer, but when it says

> When called from within the same class or module scope the receiver can be `self` or implicit

then the scopes referred to are regions with the same `self`. `self` within an instance method refers to instances of the class. `self` within a class method refer to the class itself. Hence the behavior you are seeing show up.

This behavior is shared with Ruby, for example.

---

<div class="post-metadata">

**Author:** ![ya55en](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ya55en/32/1819_2.png) [@ya55en](https://forum.crystal-lang.org/u/ya55en)\
**Post date:** [November 24, 2022, 7:27pm UTC](https://forum.crystal-lang.org/t/class-methods-invocation-works-unexpectedly/5131/4 "2022-11-24T19:27:44Z")

</div>

@Blacksmoke16, @yxhuvud: thank you so much!

I get it now. (You could probably sense my python background ;)

For people like me, an example in the documentation including the `self.class.class_method` approach might be of help. (I wonder if it makes sense to try contributing something to that page?)
