Skip to main content

Command Palette

Search for a command to run...

A comparison of multiple generative AI tools when asking for Ruby on Rails code

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

Updated
5 min read
A comparison of multiple generative AI tools when asking for Ruby on Rails code
L

Senior Product Engineer, working in Ruby and Rails. Passionate about idea generation, creativity, and programming.

I curate the Short Ruby Newsletter.

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

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 response:

ChatGPT response

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

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

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 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

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 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

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

So I said to Cody:

Followup with Cody AI describing that it does not work as it shows one single dot

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

Again this code will not show anything so I asked again Cody 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

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

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

Github Copilot Chat

Github Copilot Chat 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

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 is a Hashnode tool described as "The AI search companion, optimized for developers"

Here is the response I got:

Rix response

(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 some other things that you did not specifically ask for.


Enjoyed this article?

Join my Short Ruby News newsletter for weekly Ruby updates. Also, check out my co-authored book, LintingRuby, for insights on automated code checks. For more Ruby learning resources, visit rubyandrails.info.

G

Aside from the AI, I'd take a step back and ask what the benefit is of translating clunky html/css into ruby. The result is harder to read than the html/css, reminds me of why I use ruby less these days, because it wants to wrap the front end in back end code

L

Thank you for your comment. This is a subjective choice in some way.

Ruby does not require you to do that, as you probably know, but I want to say this out loud to anyone that might read this.

Rails with .erb, for example, allows anyone to choose how much they want to write in Ruby and how much they want to write in HTML, with the majority being HTML/CSS.

In my case, I am a backend engineer, so when I think about Frontend work, I want to generate it automatically. I think about frontend in pieces that I want to be stable, and then I want to compose them in what is needed. Thus I, for example, like a lot Phlex and ViewComponent, but I understand that this is a personal choice that I have due to the way I see the interface.

The option to go more toward HTML or to try to use Ruby to write FE components has more to do with a mental model of how we see a web app built. Of course, I think there are more pros than cons in choosing to build the UI composable by using objects, but in this specific case, it does not matter if that code is copied in all files or put in a shared partial and included in all files. It is small enough to allow almost all choices.

In my case, I wanted it to be a helper to make sure that any other developer that wants a separator with 3 dots will use this one and not create a slightly different flavor. So, I put this into a helper because the project doesn't have ViewComponent or anything else.

1

How to use AI/LLM to write Ruby and Rails code

Part 5 of 5

In this series, I am exploring using AI/LLM/GPT to help me write Ruby code most of the time while implementing a feature in a Ruby on Rails app.

Start from the beginning

Review Ruby on Rails Code using Rubymine AI and ChatGPT

Examining Ruby on Rails Code via Rubymine AI and ChatGPT

More from this blog

All about code - Ruby and Rails technical content written by Lucian Ghinda

102 posts

I write here quick thoughts, ideas, tips, and learnings about programming, programmers, and building software. Most of my focus is on Ruby, Rails, Hotwire, and everything about web applications.