3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Elixirで文字列をモジュールに変換する

Last updated at Posted at 2018-03-16

mixのタスクで受け取ったパラメータをモジュールに変換して何か実行したい場合とか用メモ。

"some_module"SomeModule に変換したい場合。

string = "some_module"
module = string |> Macro.camelize()
m = Module.concat([Elixir, module]) # => SomeModule

Module.concatを使えば変換してくれるけど、モジュール名がネストしていない場合は暗黙的に付加されている Elixir を先頭に指定してやれば問題ない。

上の例なら、SomeModuleがexec/0という関数を持っていれば、

m.exec()

で関数を実行できる。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?