Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is =~ operator in elixir

Tags:

elixir

From the documentation I understand how =~ operator works to match regex, but I don't understand the general use of this operator.

For example, what does "foo" =~ "foo" mean? How is it different from "foo" == "foo"?

like image 701
noscreenname Avatar asked Jun 21 '17 09:06

noscreenname


People also ask

What does |> mean in Elixir?

This is the pipe operator. From the linked docs: This operator introduces the expression on the left-hand side as the first argument to the function call on the right-hand side. Examples.

How do you divide in Elixir?

In Elixir, division using the division operator (/) always results in a float no matter whether the operands are integers or floats. So 5 / 2 is 2.5 and 8 / 2 is 4.0. Javascript behaves similarly, but Javascript has a single numerical data type.


1 Answers

It's not documented on that page, but it's documented in Kernel.=~/2 that when the RHS is a string, =~ checks if LHS contains RHS:

iex(1)> "foo" =~ "f" true iex(2)> "foo" =~ "o" true 

It does not implicitly convert RHS to regex:

iex(3)> "foo" =~ "." false 
like image 101
Dogbert Avatar answered Sep 20 '22 08:09

Dogbert