15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

  • Find the library functions to do the following, and then use each in iex. (If there’s the word Elixir or Erlang at the end of the challenge, then you’ll find the answer in that set of libraries.)

    • Convert a float to a string with 2 decimal digits. (Erlang)
    • Get the value of an operating system environment variable. (Elixir)
    • Return the extension component of a file name (so return .exs if given "dave/test.exs" (Elixir)
    • Return the current working directory of the process. (Elixir)
    • Convert a string containing JSON into Elixir data structures. (Just find, don’t install)
    • Execute an command in your operating system’s shell
A Possible Solution
# Convert a float to a string with 2 decimal digits.
iex> :io.format("~.2f~n", [2.0/3.0])
0.67
:ok

# Get the value of an operating system environment variable.
iex> System.get_env("HOME")
"/Users/dave"

# Return the extension component of a file name
iex> Path.extname("dave/test.exs")
".exs"

# Return the current working directory of the process
iex> System.cwd
"/Users/dave/BS2/titles/elixir/Book/yourturn/ModulesAndFunctions"

# Convert a string containing JSON into Elixir data structures
# There are many options. Some, such as https://github.com/guedes/exjson,
# are specifically for Elixir. Others, such as https://github.com/hio/erlang-json
# are Elnag libraries that are usable from Elixir.

# Execute an command in your operating system's shell
iex> System.cmd("date")
"Sun Jul 14 15:04:06 CDT 2013\n"
04 Sep 2013, 14:22
Generic-user-small

Fred Hsu (2 posts)

Typo on the last line of the comment about JSON libraries.. “Elnag libraries”

21 Sep 2013, 03:37
Ac_simpsonized_pragsmall

Antonio Carlos da Graça Mota Durão de Souza (5 posts)

The worse answer:

# Return the extension component of a file name

String.split("dave/test.exs", ".") |> tl |> hd

AC

30 Nov 2013, 08:35
Generic-user-small

Kit Ko (2 posts)

# Return the extension component of a file name

The search function from elixir-lang.org/docs is so limited that I have to resort to Google.

site:elixir-lang.org/docs extension

10 Mar 2014, 23:45
Nathan-6kites_pragsmall

Nathan Feaver (8 posts)

In the latest version of Elixir (0.12.4), using a shell command is done through the Mix module:

IO.write "Number of files in the current directory: "
Mix.Shell.cmd("ls -v | wc -l", &IO.write(&1))

# This method should be a shorthand but didn't work for me:
Mix.Shell.IO.cmd("ls -v | wc -l")
11 May 2014, 02:30
Generic-user-small

Thomas Chik (1 post)

erlang:float_to_list also converts float to string

iex> :erlang.float_to_list(2.0/3.0, [{:decimals, 2}])
'0.67'
15 Jun 2014, 11:32
Generic-user-small

Gary Rennie (1 post)

Return the extension component of a file name

pwd worked in IEX for me.

iex(5)> pwd
/home/gazler/development/elixir/programming-elixir/ch06

Docs

08 Jul 2014, 19:58
Patrick_pragsmall

Patrick Oscity (13 posts)

Technically, the first task asks us to “Convert a float to a string with 2 decimal digits”, but your solution prints it:

 :io.format("~.2f~n", [2.0/3.0])

to obtain an actual string you can use

:io_lib.format("~.2f~n", [2.0/3.0]) |> hd

since the pipe does not appear in the book before, maybe you should write

[str] = :io_lib.format("~.2f~n", [2.0/3.0])

I think you should either update the task in the book to “print a float” or change the proposed solution here.

19 Nov 2014, 13:23
Profile_pragsmall

Michele Costa (2 posts)

The System.cmd/1 function is gone in Elixir version >1, replaced by System.cmd/3.

You now have to call Erlang :os.cmd/1 function instead.

24 Dec 2014, 12:08
Generic-user-small

Pierre Sugar (55 posts)

I especially had problems with System.cmd. I was using ls like System.cmd(ls, ["-l"]). The directory content was listed but I always got a weird error. Finally I realized that ls is a command itself and in course of System.cmd the command has to be called like so `System.cmd(“ls”, [“-l”]).


IO.puts "* Float to String"
IO.puts :erlang.float_to_list(1.2356, [{:decimals, 2}])
IO.puts "\n* Retrieve system variable"
IO.puts System.get_env["LANG"]
IO.puts "\n* Retrieve file extension"
IO.puts Path.extname("guess.exs")
IO.puts "\n* Print current working directory"
IO.puts System.cwd
IO.puts "\n* Find JSON library"
IO.puts "There are a lot of JSON libraries:"
IO.puts "exjson, exjsx, jaz, joken, jsex, json, jsx, jsxn, poison"
IO.puts "They all can be found at https://github.com/h4cc/awesome-elixir#json"
IO.puts "\n* Call a system command"
IO.puts inspect System.cmd("ls", ["-la"])

27 Jan 2015, 15:16
Generic-user-small

Andres Gallego (2 posts)

System.cmd(“date”) for execute a command in my operating system’s shell doesn’t work for me, instead I used

System.cmd("date", [])
  You must be logged in to comment