thojanssens1
Why does IO.inspect prints literally "\n" instead of a return line?
When executing the following:
IO.inspect Inspect.Ecto.Query.to_string(from u in User, where: u.id == ^1)
special characters such as \n are printed literally.
"from u0 in MyApp.User,\n where: u0.id == ^1"
However, if I use IO.puts:
IO.puts Inspect.Ecto.Query.to_string(from u in User, where: u.id == ^1)
the output is as needed:
from u0 in MyApp.User,
where: u0.id == ^1
Anyone has any idea why?
Marked As Solved
kip
inspect/2 and its friend IO.inspect/2 are intended as debugging aids. So its very helpful to encode non-printing characters so a developer can see clearly the content. Noting that the Inspect protocol is implemented per data type - what you are describing is the behaviour for a String.t().
As you are seeing, the String.Chars protocol is intended to produce a string for “human” consumption hence implementations of the protocol (including for String.t() doesn’t encode non-printing characters.
Also you should be just able to do:
IO.inspect from(u in User, where: u.id == ^1))
since IO.inspect/2 will leverage the Inspect protocol implementation defined for %Ecto.Query{}.
Also Liked
NobbZ
That function is not part of the publicly documented API and therefore might be removed at any time.
thojanssens1
Because it’s easier to read. When there is an error with your query, Ecto also pretty-prints them (that’s where I got that function from).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









