Here are a few examples using ChatGPT to write some programs in Ruby:
(1) Write a ruby program to calculate the distance between two points on the surface of the earth given polar coordinates
ChatGPT
require 'math'
# Haversine formula to calculate distance between two points on the surface of a sphere
def distance(lat1, lon1, lat2, lon2)
dlat = (lat2 - lat1).to_radians
dlon = (lon2 - lon1).to_radians
a = Math.sin(dlat / 2)**2 + Math.cos(lat1.to_radians) * Math.cos(lat2.to_radians) * Math.sin(dlon / 2)**2
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
R = 6371 # Radius of the earth in kilometers
R * c
end
# Example usage
puts distance(51.5074, 0.1278, 35.6895, 139.6917) # Outputs: 14707.23556565625
Notice that CGPT used long and lat versus polar coordinates, so I asked for a method in Ruby to covert polar coords to long and lat and got another method
Then I asked....
(2) write a program in ruby to calculate the average velocity of hydrogen atoms in a boiling water on earth at sea level.
ChatGPT
# Determine the temperature of the boiling water in degrees Celsius
temp_c = 100
# Convert the temperature to kelvins
temp_k = temp_c + 273.15
# Calculate the average kinetic energy of the hydrogen atoms
boltzmann_constant = 1.38 * 10**-23
ke = (3.0/2) * boltzmann_constant * temp_k
# Calculate the average velocity of the hydrogen atoms
mass_hydrogen = 1.67 * 10**-27
velocity = Math.sqrt((3 * boltzmann_constant * temp_k) / mass_hydrogen)
puts "The average velocity of hydrogen atoms in boiling water at sea level is #{velocity} meters/second."
Of course, any normal engineer would not take these programs at face value and would check them (at this point in AI evolution), but between the explanations (not posted here) and the code, it's a good start.
I also tried other programs for example:
(3) Write a program in ruby to bind to a unix socket, listen and receive a JSON object, and return a status code
require 'socket'
require 'json'
# Create a socket and bind it to a Unix domain socket
socket = Socket.new(:UNIX, :STREAM)
socket.bind(Socket.pack_sockaddr_un('/path/to/socket'))
# Listen for incoming connections
socket.listen(5)
# Accept an incoming connection
connection, _ = socket.accept
# Read the incoming data as a JSON object
data = connection.read
json = JSON.parse(data)
# Return a status code
connection.write(200)
# Close the connection
connection.close
... same question using bash
# Create a Unix domain socket and bind it to a specified path
nc -lU /path/to/socket &
# Read the incoming data as a JSON object
data=$(nc -U /path/to/socket)
json=$(echo $data | jq -r '.')
# Return a status code
echo "200" | nc -U /path/to/socket
# Close the connection
pkill nc
ask again.... same question
#!/bin/bash
# Create a socket and bind it to a Unix domain socket
socat UNIX-LISTEN:/path/to/socket,fork - &
# Wait for an incoming connection
while true; do
# Read the incoming data as a JSON object
data=$(nc -U /path/to/socket)
json=$(echo $data | jq -r '.')
# Return a status code
echo 200 > /path/to/socket
done
and again....
#!/bin/bash
# Create a Unix domain socket and bind it to the specified path
socat UNIX-LISTEN:/path/to/socket,fork - &
# Accept an incoming connection
connection=$(socat - UNIX-CONNECT:/path/to/socket)
# Read the incoming data as a JSON object
data=$(echo "$connection" | jq -r .data)
# Return a status code
echo "HTTP/1.1 200 OK" >&3
# Close the connection
exec 3<&-
same question in C++
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main() {
// Create a socket and bind it to a Unix domain socket
int socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, "/path/to/socket", sizeof(addr.sun_path) - 1);
bind(socket_fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr));
// Listen for incoming connections
listen(socket_fd, 5);
// Accept an incoming connection
int connection_fd = accept(socket_fd, NULL, NULL);
// Read the incoming data as a JSON object
char buffer[1024];
ssize_t bytes_read = read(connection_fd, buffer, sizeof(buffer));
std::string data(buffer, bytes_read);
json j = json::parse(data);
// Return a status code
std::string response = "HTTP/1.1 200 OK\n\n";
write(connection_fd, response.c_str(), response.size());
// Close the connection
close(connection_fd);
close(socket_fd);
return 0;
}
Yes, this is not production code, but it's a good start for folks... and with a click of the mouse CGPT will do it again, often differently.
Same question asking for C#
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Newtonsoft.Json;
namespace UnixSocketExample
{
class Program
{
static void Main(string[] args)
{
// Create a socket and bind it to a Unix domain socket
Socket socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
EndPoint endpoint = new UnixEndPoint("/path/to/socket");
socket.Bind(endpoint);
// Listen for incoming connections
socket.Listen(5);
// Accept an incoming connection
Socket connection = socket.Accept();
// Read the incoming data as a JSON object
StreamReader reader = new StreamReader(new NetworkStream(connection));
string data = reader.ReadToEnd();
dynamic json = JsonConvert.DeserializeObject(data);
// Return a status code
StreamWriter writer = new StreamWriter(new NetworkStream(connection));
writer.WriteLine("HTTP/1.1 200 OK");
writer.WriteLine();
writer.Flush();
// Close the connection
connection.Close();
socket.Close();
}
}
}