-1

I'm a newbie in Crystal.

I'm trying to return self in Crystal, like in this Python:

class Something:
    def some_method():
        # do something
        return self
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
bichanna
  • 954
  • 2
  • 21
  • What was the problem when you tried to do it? – mkrieger1 Jan 18 '22 at 22:45
  • There are two great resources for learning crystal. The first is the crystal docs: https://crystal-lang.org/docs/ -- both an extensively documented API and language reference doc are there. The second resource is to look at Ruby. Crystal draws a lot of it's syntax and style from Ruby, and while the examples don't always translate 1:1, they style of programming often does. – voxobscuro Mar 07 '22 at 23:48

1 Answers1

2

Here is a little extended example. say_hello is called by self.say_hello. Just defining self returns the object/instance itself, like expected.

% cat file.cr
class Something
  def say_hello
    puts "Hi"
  end

  def some
    self.say_hello
  end

  def some_method
    self
  end
end

s = Something.new

s.some

puts s.some_method

Output

% crystal run file.cr
Hi
#<Something:0x102b6de80>
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29