Skip to content

Instantly share code, notes, and snippets.

@theoretick
Last active March 2, 2016 18:40
Show Gist options
  • Save theoretick/bdd8f512ec438531115d to your computer and use it in GitHub Desktop.
Save theoretick/bdd8f512ec438531115d to your computer and use it in GitHub Desktop.
Private imports in ruby versus elixir
iex(1)> defmodule Foo do
...(1)> defp bar do
...(1)> "bar"
...(1)> end
...(1)> end
iex:2: warning: function bar/0 is unused
{:module, Foo,
<<70, 79, 82, 49, 0, 0, 3, 168, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 94, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
{:bar, 0}}
iex(2)> defmodule Derp do
...(2)> import Foo
...(2)> def herp do
...(2)> bar
...(2)> end
...(2)> end
** (CompileError) iex:5: function bar/0 undefined
(stdlib) lists.erl:1337: :lists.foreach/2
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
(iex) lib/iex/evaluator.ex:117: IEx.Evaluator.handle_eval/5
[4] pry(main)> module Foo
[4] pry(main)* private
[4] pry(main)* def bar
[4] pry(main)* "bar"
[4] pry(main)* end
[4] pry(main)* end
=> :bar
[5] pry(main)> class Derp
[5] pry(main)* include Foo
[5] pry(main)* def herp
[5] pry(main)* bar
[5] pry(main)* end
[5] pry(main)* end
=> :herp
[6] pry(main)> Derp.new.herp
=> "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment