Turning function argument in iex into string

Hi guys,

I’m trying to create a function that can be called from iex by just passing in an argument but without quotation marks. I have a function in my module that takes an argument and calls an API endpoint with the argument attached to the end of therequest. Something like this:
> def make_request(word) do
> get("www.example.com/api?#{word}"
> end

I want to be able to simply pass in “word” without quotation marks. Like this:
iex> make_request(word)
Passing in the “word” without quotations just makes iex think that word is a variable so that obviously doesn’t work and i’ve tried a few other things but most are just dumb attempts. The ultimate goal is to avoid typing the “” every time I want to use the function in the shell.

I feel like there is an easy solution to this but I’ve been staring at it so long that it just doesn’t even make sense anymore lol

Is it possible to accomplish this??

If you want to pass in a string to your function, you have to use quotes, such that elixir can know it is a string.

This is true for every single language I am aware off.

1 Like

Like @NobbZ said, in this instance the only way of telling the elixir compiler that you are passing a string is by using double quotes.

However, if you are designing a DSL and want your language to have lesser noise. You can use macros to get around this.

defmodule Dan do
  def make_request(word) do
    IO.puts word
  end

  defmacro make_request_macro({id, _, _}) do
    id |> to_string |> make_request
  end
end

defmodule Test do
  def test do
    #Dan.make_request("awesome")
    require Dan
    Dan.make_request_macro(foo)
    Dan.make_request_macro(awesome)
  end
end
Test.test
# => foo
# => awesome

In this instance you are matching on the AST passed to your macro and getting the atom which is then converted to a string and passed on to your function

1 Like

I’d not suggest to go this route, since this can really obfuscates the program and its intend.

You remember the first rules of writing a macro?

2 Likes

could you explain why it’s important to you? Is it literally to avoid extra key strokes for "? If so, maybe use :word which is one less keystroke :slight_smile: Otherwise, if you want to pass word as in make_request(word) but you don’t want it to be treated as a variable then you’ll need to use a macro.

1 Like

:word is something different than "word", which again differs from word.

When I read world = "foo"; make_request word I do not expect to see word, I expect to see foo.

1 Like

yes, obviously it’s different. However, because he does: "www.example.com/api?#{word}" then the outcome is gonna be the same if word is "foo" or :foo (or 'foo' etc). But yeah, I think we need more information from the OP what exactly he wants to achieve and why.

1 Like

If you actually just want to look up documentation, there is a horrible hack you can use.

A bare string that starts with a Capital Letter is an Atom in Elixir.

iex(1)> i Word
Term
  Word
Data type
  Atom
Raw representation
  :"Elixir.Word"
Reference modules
  Atom
Implemented protocols
  IEx.Info, Inspect, List.Chars, String.Chars

You could write your function like this

def make_request(word) when is_atom(word) do 
   evil = word |> Atom.to_string |> String.split(".") |> Enum.at(1) |> String.downcase
   get("www.example.com/api?#{evil}"
end

But that is really evil. If you want to add your own commands to iex this is an example of how to do that in a relatively seamless way. https://github.com/philosophers-stone/ehelper

Note: ehelper isn’t working to find erlang docs on my brew based OS/X box at the moment.

2 Likes

hi, thanks for taking the time to respond to what i now realize was a really silly question. Yes, there is no reason for trying to do this other than having to avoid typing the “” when i call the function. I could just have an atom passed in and convert it to a string.

Anyway, I’ll ultimately be running the program from the command line (not iex) so I should be able to pass in an argument and convert it to a string yeah?

thanks again for trying to help, one of the reasons elixir is so awesome is cuz the community offers assistance even when noobs like myself ask stupid questions

When you run your program from CLI, you will have access to System.argv(), which is a list of strings, as specified on the CLI. If you do an escript, you will even get it passed to your entrypoint as y list of strings.

2 Likes