ChatGPT for Google Chrome Extension - A Good Reason to Love ChatGPT as a Programming Assistant

Becoming a bigger fan of ChatGPT with each passing day.... loving OpenAI for sure.

I installed the Chrome extension ChatGPT for Google (reference below) and it places the ChatGPT response right beside the Google search results.

Then I type this in the Chrome toolbar:

write ruby method to calculate the distance between earth and the moon based on unixtime

The results, pictured here:

The code:

def calculate_distance(unix_timestamp)
  # Earth-Moon distance varies over the course of a month due to the elliptical shape of the Moon's orbit
  # We can use the timestamp to calculate the percentage of the way through the lunar cycle the current time is
  # and use that to estimate the distance
  days_since_new_moon = (unix_timestamp / 86400) % 29.53 # 86400 seconds in a day, 29.53 days in a lunar cycle
  percent_through_cycle = days_since_new_moon / 29.53
  # The distance between the Earth and Moon ranges from 356,500 km at its closest to 406,700 km at its farthest
  min_distance = 356500
  max_distance = 406700
  # We can use linear interpolation to estimate the distance based on the percentage through the cycle
  distance = min_distance + (max_distance - min_distance) * percent_through_cycle
  return distance
end

Open the rails console and copy-and-paste this method and run it:

irb(main):016:0> moon_distance(0)
=> 403499.25
irb(main):017:

What is there not to love about this? Now type this in the browser bar:

write ruby method to convert a data string to unixtime

The code....

def convert_to_unixtime(datestring)
  # Parse the input string using the DateTime class
  datetime = DateTime.parse(datestring)

  # Convert the DateTime object to a Unix timestamp
  unixtime = datetime.to_time.to_i

  # Return the Unix timestamp
  return unixtime
end

So we do this in the console:

irb(main):030:0> moon_distance(convert_to_unixtime("2023-01-07 14:30:00"))
=> 372830.42898668605

Or we could have asked for this directly:

write ruby method to calculate the distance between earth and the moon based on a date string

Which results in this code:

def calc_moon_distance(date_str)
  require 'date'
  require 'time'

  # Parse the date string
  date = Date.parse(date_str)

  # Calculate the number of days since the epoch
  days_since_epoch = (date - Date.new(2000, 1, 1)).to_i

  # Calculate the distance between Earth and the Moon in kilometers
  distance = (384400 + (0.5 * days_since_epoch)).round(2)

  # Return the distance
  return distance
end

But the results are different than the first method:

irb(main):047:0> calc_moon_distance("2023-01-07 14:30:00")
=> 388603.5

This is a good way to error check and/or validate code.

We can use different methods generated by ChatGPT and compare results quickly. If the results match, we have more confident in the results. We can rephrase our requirement. We can specify our requirements more precisely. We can learn to get the most of our AI assistants.

Some will argue that ChatGPT is not perfect. Well, it's like an kind of AI singularity, isn't it. At first, it's a child but over time, it gets more and more capable. AI is a tool and in the hands of capable developers, it is a very powerful tool.

It's really up to us, the human users (developers, sys admins), to control the use of the tool and to learn and to know how to generate confidence in the AI's coding and how to check the results.

Tools are only as good as the skill of the person who uses them.

So, in my view ChatGPT for Google, displaying the ChatGPT response right next to Google search results is a very nice Chrome extension.

3 Likes

I'm glad you're enjoying using ChatGPT! It's definitely a useful tool for generating code quickly, but it's important to keep in mind that it's not always 100% accurate and that it's important to review and test the code it generates before using it in production. Comparing the results of multiple methods can be a helpful way to validate the results and increase your confidence in them.

As with any tool, it's important to use it responsibly and to have a good understanding of how it works in order to get the most out of it. Continuously learning and improving your skills as a developer is always a good idea, and working with AI tools like ChatGPT can be a great way to do so.

In the cases you mentioned, it appears that the two methods for calculating the distance between the Earth and Moon produced different results. This could be due to a number of factors, such as the assumptions or formulas used in the different methods. It's always a good idea to thoroughly test and debug your code to ensure that it is accurate and reliable.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.