mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Passing Around And Using Modules as an elixir til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_796 TILs and counting..._
|
_797 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -145,6 +145,7 @@ _796 TILs and counting..._
|
|||||||
- [List Functions For A Module](elixir/list-functions-for-a-module.md)
|
- [List Functions For A Module](elixir/list-functions-for-a-module.md)
|
||||||
- [Listing Files In IEx](elixir/listing-files-in-iex.md)
|
- [Listing Files In IEx](elixir/listing-files-in-iex.md)
|
||||||
- [Match On A Map In A With Construct](elixir/match-on-a-map-in-a-with-construct.md)
|
- [Match On A Map In A With Construct](elixir/match-on-a-map-in-a-with-construct.md)
|
||||||
|
- [Passing Around And Using Modules](elixir/passing-around-and-using-modules.md)
|
||||||
- [Pattern Matching In Anonymous Functions](elixir/pattern-matching-in-anonymous-functions.md)
|
- [Pattern Matching In Anonymous Functions](elixir/pattern-matching-in-anonymous-functions.md)
|
||||||
- [Quitting IEx](elixir/quitting-iex.md)
|
- [Quitting IEx](elixir/quitting-iex.md)
|
||||||
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
|
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
|
||||||
|
|||||||
38
elixir/passing-around-and-using-modules.md
Normal file
38
elixir/passing-around-and-using-modules.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Passing Around And Using Modules
|
||||||
|
|
||||||
|
A module is a bag of functions. When we define a module, we are tying it to
|
||||||
|
an atom. If we pass around the atom that references this module, then we can
|
||||||
|
use it to call its functions.
|
||||||
|
|
||||||
|
For example, consider two types of greetings:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
defmodule Hello do
|
||||||
|
def get_greeting do
|
||||||
|
"Hello, World!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Hola do
|
||||||
|
def get_greeting do
|
||||||
|
"Hola, Mundo!"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
And a generic greeting module that accepts a language module:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
defmodule Greeting do
|
||||||
|
def say_hello(language_module) do
|
||||||
|
language_module.get_greeting
|
||||||
|
|> IO.puts
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Greeting.say_hello(Hello) # => "Hello, World!"
|
||||||
|
Greeting.say_hello(Hola) # => "Hola, Mundo!"
|
||||||
|
```
|
||||||
|
|
||||||
|
The module reference that we pass in to `Greeting.say_hello` can be used to
|
||||||
|
invoke the `get_greeting` function.
|
||||||
Reference in New Issue
Block a user