4

I'm working through Dave Thomas' Programming Elixir and I'm attempting some examples from the list chapter.

When I'm learning a language I prefer to stay as basic as I can by running <language executable> <script file>. In this case I'm running elixir reduce.exs

contents of reduce.exs:

require IEx;

defmodule MyList do
  def reduce([], memo, _), do: memo
  def reduce([head | tail], memo, func) do
    IEx.pry
    reduce(tail, func.(head, memo), func)
  end
end

ExUnit.start()
defmodule MyListTest do
  use ExUnit.Case

  def test do
    assert 10 == MyList.reduce([1,2,3,4], 0, &(&1 + &2))
  end
end

IO.puts(MyListTest.test())

When run the console outputs:

Cannot pry #PID<0.70.0> at reduce.exs:9. Is an IEx shell running?

I assume I am completely misunderstanding some core concepts, but I'm not entirely sure what they are.

My expectation is that the program would just drop into an iex session when it hits the execution of IEx.pry. Given iex is in the elixir core libraries, I thought the require IEx would be enough to use pry.

Do I need to use IEx.pry/3? Do I need to run a separate instance of iex and somehow connect the two nodes together?

Just evaluating the code by running iex reduce.exs runs the file, but it does not show the test output.

Feel free to correct any and every silly assumption I've made.

4

1 回答 1

4

您收到此错误是因为IEx需要运行。使用该命令iex reduce.exs将允许您输入代码以及在IEx.pry源文件中的位置。

要从那里继续执行,respawn请键入 shell。它会询问您是否要在每次递归时允许 pry,但最终会打印出测试结果。

您之前没有看到测试输出的原因是IEx.pry执行停止,因此您的测试函数没有返回并且IO.puts调用没有完成。

于 2017-03-19T05:20:04.737 回答