First of all, I want to say sorry because it’s probably a really dumb question…
I’m starting to learn Elixir and i come from a different background (Typescript) so i’m having some difficulty understanding the syntax of modules and methods.
With that being said, what does def mean when declaring a method? And why it has to come with do in the end?
Hello, there are no methods in Elixir. The organization is “functions” inside “modules”. “def” is for “define” a function. And “do” is for “open” a code block, like “{ }” in TypeScript.
The syntax is inspired by ruby. You can compare def to public function … you see in many OO languages (not in js/ts though), while do … end are like { … }.
If you look at ruby code and elixir code they look very similar. There are a lot of similar keywords and other syntactical elements used. That’s about it though. It does not mean elixir works similar than ruby, to the contrary.