A comparison of multiple generative AI tools when asking for Ruby on Rails code
Tags: ruby, rails, ai, developers
Published: 2023-07-07 05:07:41

Comparing Rix, Cody, Phind, Copilot Chat, and ChatGPT's responses to a basic Ruby on Rails query.

I asked the following query for multiple AI code generator tools:

```markdown
Please transform this code:

<div class="flex justify-center py-6 mt-8 mb-3" role="separator">
  <span class="dot"></span>
  <span class="dot"></span>
  <span class="dot"></span>
</div>

into a Rails Helper with content tags
```

I tried to make the request a mix of simple wording while also providing a small context by specifying the `content tags` and mentioning `Rails Helper`.

## Chat GPT

Here is the [ChatGPT](https://chat.openai.com) response:


![ChatGPT Response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iwxetf3rq4k368e1rly.png)



It works, it will display the HTML as required.

My main review here is that I would use `concat` instead of `join.html_safe`

So I did a follow-up:


![Followup with ChatGPT asking to remove html_safe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85ytn6sff1r9c8tp4vet.png)


Of course, this was not what I was looking for, but I also agree that my request was not very specific. I did this intentionally to keep the conversation casual, and not suggest a specific solution. I also notice that it changed the second param of `content_tag` from an empty string to `nil`. I did not request this specific change but I assume asking `refactor` triggered a wider change.

I again follow up with the request to remove `safe_join`:

![Follow up with ChatGPT asking to remove the safe_join](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8aclel45ez33ya4p769d.png)


And this indeed is the solution that I think is a bit close to what I wanted. I don't think `.map` is needed there and I usually like helpers to make things explicit so I would have written 3 lines of `concat(content_tag)` but I think in general the solution is usable.

## Phind

[Phind](https://www.phind.com) describes themselves as *"The AI search engine for developers"*.

Here is the response from Phind:

![Response from Phind showing a DivHelper that has a method dot_separator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93s1cvhayn8kxtaldi50.png)

I was quite happy with the result:

* Good name `dot_separator`
    
* I like the idea that it extracted the actual dot to a private method `dot_span` but I think the name could have been improved to only `dot`.
    
* I like that it used `concat`
    
* And I welcome the explicitness of having 3 lines of `contact(dot_span)` instead of `3.times { contact(dot_span) }`
   
I did not feel here that I need to follow up so I moved to the next tool.

## Cody

[Cody](https://docs.sourcegraph.com/cody) is an AI assistant from Source Graph - *"Cody is an AI coding assistant that writes code and answers questions for you by reading your entire codebase and the code graph"*

Here is the first response from Cody:

![First response from Cody AI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfu2auj5ieieekgsjg1h.png)


This will not work as intended, as it only displays one single dot.

So I said to Cody:

![Followup with Cody AI describing that it does not work as it shows one single dot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2amh7yrpfwg30lyhllk.png)

Now this code will not display anything, so I asked Cody to fix it:

![Second followup with Cody AI asking to fix the bug where it does not show anything](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k0bjufefrwyd2edfgufh.png)


Again this code will not show anything so I asked Cody again to fix it, but now I asked also to explain why it does not work. This is a trick with LLMs that will usually give better results because it brings more context about what is not working and the cause of it:

![Third follow up with Cody AI saying again it does not work and asking to fix it](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4i2asisnilwvq2mi5wbt.png)

Now this works - it will display 3 dots, but it is still a version that I don't like as it uses `html_safe`. So I asked for a refactor:

![Asking Cody AI to refactor to remove usage of html_safe](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aor0w4axa5ftisj4qigl.png)

This works and it is a solution that I consider ok(ish).

## Github Copilot Chat

[**Github Copilot Chat**](https://github.com/github-copilot/chat_waitlist_signup) is *"A ChatGPT-like experience in your editor with GitHub Copilot chat"* that I installed in a VScode Insiders version.

Here is the response I got from it:

![Github Copilot Chat response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5803kqlvz35o76jao5w5.png)

First, this solution works. Then I like that it tried to give a good name, `separator_dots`, and I appreciate that it used `concat` instead of first trying to use `html_safe`

Copilot Chat is a version of the Open AI GPT-4 model, I think, but probably has a better system prompt.

## Rix

[Rix](https://hashnode.com/rix) is a Hashnode tool described as *"The AI search companion, optimized for developers"*

Here is the response I got:


![Rix response](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4dolbzkdls6btm7qjxrh.png)

## (temporary) conclusion

In the context of Ruby programming, AI code generator tools offer varying results and sometimes require follow-ups to achieve the desired output. While some tools, like Phind and Github Copilot Chat, provide satisfactory solutions quicker, others may need additional guidance. It is essential to thoroughly review the generated code to ensure security and avoid unintended changes.

## Word of caution

**Do not feed private information:** I used this with a piece of code that does not show any private information about the project I worked with.

**Be careful what code you feed into these tools:** When using generative AI tools - I only add those to my side projects and most of the time when I know that I will anyhow make the project open source. I would not use them or give them access to a client code base unless explicitly accepted by the client in a contract.

**Be careful of security issues:** Also please be careful of copying/pasting code generated by AI in your project and running that code on servers. Nobody can guarantee that the code does not have security issues.

**Read all the generated code, not just the changed parts:** One last point, if you are asking for refactoring, read every time the entire code and not only the lines that you required to be changed. Sometimes the AI will change other things you did not specifically ask for.

---

Enjoyed this article?

Join my [**Short Ruby News**](https://shortruby.com/) **newsletter** for weekly Ruby updates. Also, check out my co-authored **book**, [**LintingRuby**](https://lintingruby.com/), for insights on automated code checks. For more Ruby **learning resources**, visit [**rubyandrails.info**](http://rubyandrails.info)**.**