thojanssens1

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

kip

ex_cldr Core Team

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

NobbZ

That function is not part of the publicly documented API and therefore might be removed at any time.

thojanssens1

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).

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement