Glad to be using Ruby since 2005
Published: 2023-04-14 11:41:22

---
title: Glad to be using Ruby since 2005
published: true
date: 2023-04-13 14:01:11 UTC
tags: 
canonical_url: https://blog.chetanmittaldev.com/glad-to-be-using-ruby-since-2005
---

I randomly reached [OCaml](https://ocaml.org/) while searching some stuff on Google. I had heard of this software programming language before during my Uni days, however, I never learned and used it.

Maybe [I picked up a scripting language TCL more quickly](https://blog.chetanmittaldev.com/how-to-download-and-install-tcltk-in-2023-to-build-guis) than some typed functional language OCaml.

Anyways, I thought it would be a good idea if I pick up OCaml today and try writing a simple program in it that I had written using [Ruby](https://www.ruby-lang.org/en/) earlier.

The Ruby program is very simple. It just goes to my blog by using `open-uri` gem and then just accesses the `DOM` by using `nokogiri` gem, and then just iterates through all the `img` tags and prints their _URLs with alt-text_ on the console. Here it is:-

```
require 'nokogiri'
require 'open-uri'

url = 'https://blog.chetanmittaldev.com/'

html = URI.open(url)
doc = Nokogiri::HTML(html)

image_count = 0

doc.css('img').each do |img|
  image_count += 1
  puts "#{img['alt']}: #{img['src']}"
end

puts "Total images: #{image_count}"

```

The above code just prints all the image links with their alt-texts from my blog as shown below:-

There are a total of 38 images as of before publishing this blog post.

![images-list](https://cdn.hashnode.com/res/hashnode/image/upload/v1681392372559/1eef6f3c-7058-4639-ae4f-87d3d18d1edc.png)

![38-images-total](https://cdn.hashnode.com/res/hashnode/image/upload/v1681392496754/27f88dba-1cb7-4998-b997-a14be9f9f4a4.png)

The same code to write in OCaml; first it is pretty hard to find in its documentation which library does what; second, configuring a program's compile build with libraries is such a pain.

And, third, each time I installed a library using its package manager called `opam`, which doesn't even try to find, fetch, and install a package's dependencies if the package you are installing hasn't been updated for a long time, I had to run two commands `$ opam user-setup install` and `$ eval 'opam config env'` that configure and evaluate respectively the OCaml environment I guess.

Anyways, I asked ChatGPT to generate a program in OCaml from my Ruby code and it generated some non-working code below:-

```
open Nethttp_client.Convenience
open Tyxml.Xml

let url = "https://blog.chetanmittaldev.com/"

let () =
  let conn = new Nethttp_client.connection url in
  let html = http_get conn in
  let doc = parse_string html in

  let image_count = ref 0 in

  List.iter (fun img ->
    incr image_count;
    let name = attrib img "alt" in
    let url = attrib img "src" in
    Printf.printf "%s: %s\n" name url
  ) (children (List.hd (children doc)));

  Printf.printf "Total images: %d\n" !image_count

```

I used OCaml's build system called `dune` to generate a new project structure as below:-

![ocaml-project-structure](https://cdn.hashnode.com/res/hashnode/image/upload/v1681392718088/e3dff260-2b90-42e3-be3b-4af533a9a2c4.png)

Then I configured my `dune` build file with the required libraries, and wrote some code:-

```
(executable
 (public_name scrape)
 (name main)
 (libraries netclient curl lwt)
 (preprocess (pps lwt_ppx)))

```

![ocaml-vscode](https://cdn.hashnode.com/res/hashnode/image/upload/v1681393971858/0bf04485-d543-4fef-b730-c0736f56049e.png)

_Oh, I forgot to mention, I spent a few hours, finding "how to install OCaml", and then installing it on my EndeavourOS._

And, when I tried to build or compile my OCaml code by running `$ dune build` then I got these errors as below:-

![dune-build-error](https://cdn.hashnode.com/res/hashnode/image/upload/v1681394130613/b017eb83-107e-4021-9d0e-e9150adb4559.png)

Hmm, I tried installing the `curl` package for OCaml:-

`$ opam install curl`

The package was "NOT FOUND". Amazing, right? `curl` is not available.

![curl-not-found-in-ocaml](https://cdn.hashnode.com/res/hashnode/image/upload/v1681394271051/6ba85cb8-f773-468e-a222-bfda542b4542.png)

What can I say, the whole ecosystem of OCaml is not right if it doesn't have the `curl` package and ChatGPT writes OCaml programs using it.

The [Ruby software programming language](https://en.wikipedia.org/wiki/Ruby_(programming_language)) is the best for me. Just 12 lines of code to scrape my blog. I am happy. Python and Javascript are equally good. Easy to read and write.