# Porting code example (Ruby to Elixir)

**URL:** <https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148>\
**Category:** Discussions\
**Created:** [October 27, 2016, 8:12pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148 "2016-10-27T20:12:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 27, 2016, 8:12pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/1 "2016-10-27T20:12:40Z")

</div>

As you may know, I am slowly/painfully writing an Elixir book.

There is a code example that I’ve written in Ruby – a simple  
robot-type “capture the flag” game. (Not a real game, just a  
coding exercise.)

I’m about to port the Ruby code to Elixir. If anyone wants to  
look/comment/help, please feel free. (Those who contribute  
will be mentioned in the acknowledgements fwiw.)

This repo will be public for at least two days:

[https://github.com/Hal9000/vexil](https://github.com/Hal9000/vexil)

Thanks,  
Hal

---

<div class="post-metadata">

**Author:** ![minhajuddin](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/minhajuddin/32/1533_2.png) [@minhajuddin](https://forum.elixirforum.com/u/minhajuddin)\
**Post date:** [October 27, 2016, 9:08pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/2 "2016-10-27T21:08:14Z")

</div>

1. Put `*.beam` in your `.gitignore`
2. `defstruct team: nil, kind: nil, move: nil, see: nil, ...` could be written as `defstruct [:team, :kind, :move, :see...]`
3. Use tuples is more idiomatic elixir instead of fixed sized lists e.g. in `def get(grid, [:red, x, y])`

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 27, 2016, 9:29pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/3 "2016-10-27T21:29:40Z")

</div>

Agreed and committed. Thank you!

Hl

---

<div class="post-metadata">

**Author:** ![johnkelly](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/johnkelly/32/4231_2.png) [@johnkelly](https://forum.elixirforum.com/u/johnkelly)\
**Post date:** [October 27, 2016, 9:44pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/4 "2016-10-27T21:44:40Z")

</div>

I think you can refactor the show\_cell function to remove the conditional and assignments.

```
   defmodule Grid do
      def show_cell(grid, xx, yy) do
        grid
        |> Grid.get({:red, xx, yy})
        |> Bot.to_string
        |> IO.write
      end
    end

defmodule Bot do
  def to_string(nil), do: "-"
end

```

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 27, 2016, 10:02pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/5 "2016-10-27T22:02:33Z")

</div>

Also excellent ideas. Committed.

---

<div class="post-metadata">

**Author:** ![romul](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/romul/32/517_2.png) [@romul](https://forum.elixirforum.com/u/romul)\
**Post date:** [October 27, 2016, 11:12pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/6 "2016-10-27T23:12:38Z")

</div>

I think, defender, fighter, scout, flag and related behavior are good to demonstrate how to work with Elixir protocols.

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 28, 2016, 11:24am UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/7 "2016-10-28T11:24:44Z")

</div>

Created first small PR. I will help you more today or/and in next days. This will depend on your response times and my free time 😄

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 28, 2016, 2:30pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/8 "2016-10-28T14:30:10Z")

</div>

That is a good idea. I don’t think I’ll introduce protocols here at first, but I  
could always refer back to the code later and show changes.

Hal

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 28, 2016, 2:33pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/9 "2016-10-28T14:33:00Z")

</div>

Merged. Appreciated. Just remember we may be in different time zones. 😉

By the way The Ruby version is not “ideal” either. The non-threaded version is  
easier to watch/understand, but the threaded one is a massive free-for-all, and I  
am not sure how I want to fix it.

Hal

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 28, 2016, 4:02pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/10 "2016-10-28T16:02:33Z")

</div>

Yes I mean I will send PR if I find some time in same day you merged previous.  
What does mean:

> FIXME pieces[team] \<\< bot

?  
I do not see a `pieces` declaration in this file …  
And this comment:

> ### line 22

😄

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 28, 2016, 6:06pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/11 "2016-10-28T18:06:12Z")

</div>

Tip: [here](http://asquera.de/blog/2015-04-10/writing-a-commandline-app-in-elixir/) you have a tutorial about creating a command line application in Elixir.  
I will create a next PR in current week (first version of this app that works from command line).

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 30, 2016, 8:32pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/12 "2016-10-30T20:32:07Z")

</div>

Hey, I added new PR.  
I rewrite Elixir code to much some guidelines.  
Now we have `Agent` that stores our game data, each bot have AI Erlang process (TODO: turn).  
Your project is now really configurable. For example you can setup 2 to 4 teams with their colours and names dynamically.  
I added some two dependencies [bunt](https://hex.pm/packages/bunt) to easy format grid and [credo](https://hex.pm/packages/credo) to keep code readability.  
Review my code and say if you don’t understand something. The code is not 10 lines, so bigger explanation may be really long. Let me know what you think about my version. If you accept it I will implement simple AI.  
I wrote that I add this PR in current week, so it’s not complete yet (AI and credo checks).  
Run code several times to see different results.

**Tip** : I created new modules inside `Vexil` module, because it’s not a “Hello World” tutorial and you are going to show good code example. In your old code you have for example: `Grid` module which may conflict in other projects if they organise code like you.

I need also write some tests.

---

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [October 31, 2016, 2:39pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/13 "2016-10-31T14:39:52Z")

</div>

Yeah, things like `defmodule Grid do` should be like `defmodule Vexil.Grid do` or so. Namespaces should always be used! 🙂

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 31, 2016, 6:44pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/14 "2016-10-31T18:44:11Z")

</div>

There are several things I don’t understand yet about this.  
Let’s talk more via email?

Namespacing is a good idea, yes.

Hal

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 31, 2016, 6:59pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/15 "2016-10-31T18:59:56Z")

</div>

No need private messages. It’s thread for your project. Feel free to ask 😄

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 31, 2016, 7:14pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/16 "2016-10-31T19:14:12Z")

</div>

First, what is your reasoning for getting rid of modules such as Bot?

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 31, 2016, 7:19pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/17 "2016-10-31T19:19:53Z")

</div>

These modules are for defining structs by `defstruct`.  
[Here](http://elixir-lang.org/getting-started/structs.html) you have details about structs.  
I think it’s best way to store data like that. Of course I can forgot about other things. Feel free to add propositions to my version of code.

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 31, 2016, 7:23pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/18 "2016-10-31T19:23:33Z")

</div>

I still don’t understand why you did away with the modules.  
Where would you store functions that manipulate those structs?

---

<div class="post-metadata">

**Author:** ![Eiji](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/eiji/32/36743_2.png) [@Eiji](https://forum.elixirforum.com/u/Eiji)\
**Post date:** [October 31, 2016, 7:34pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/19 "2016-10-31T19:34:53Z")

</div>

The main module (to manage grid) is `Vexil.Battleground`. It stores grid in `Vexil.Agent` and have functions to manipulate this grid. For example to store `Bot` or `Flag` structure. I wrote some function and split this module to some imports, so:  
`Vexil.Battleground` **imports** : `Vexil.Battleground.TeamUtils`. TeamUtils (like a normal team) **imports** `Vexil.Battleground.BotUtils` and `Vexil.Battleground.FlagUtils` and both of them **imports** `Vexil.Battleground.GridUtils`.  
**Note** : in your Ruby code `Flag` extends `Bot`, so it can move(!). I think it’s not a good to read your Ruby code, because `Flag` will not move at all.  
In my code `FlagUtils` **imports** only functions for calculate position based on corner and spawn item. Ok, but why it’s all for `Battleground`? It’s simple. `Bot` and `Flag` do not need to calculate position from corner. I think my version is easiest to understand (if you know how **import** works).  
More, flag do not think and bot do not see full grid, so they should not calculate coords. I know it’s not a bug, but it’s not easy to read - am I wrong?

---

<div class="post-metadata">

**Author:** ![Hal9000](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hal9000/32/1915_2.png) [@Hal9000](https://forum.elixirforum.com/u/Hal9000)\
**Post date:** [October 31, 2016, 7:45pm UTC](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148/20 "2016-10-31T19:45:12Z")

</div>

This seems reasonable. I will look at it further tonight. Thank you.

[Next page](https://forum.elixirforum.com/t/porting-code-example-ruby-to-elixir/2148.md?page=2)
