DEV Community

Chetan Mittal
Chetan Mittal

Posted on • Originally published at blog.chetanmittaldev.com on

Glad to be using Ruby since 2005

I randomly reached OCaml 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 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 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}"

Enter fullscreen mode Exit fullscreen mode

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

38-images-total

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

Enter fullscreen mode Exit fullscreen mode

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

ocaml-project-structure

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

Enter fullscreen mode Exit fullscreen mode

ocaml-vscode

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

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

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

Top comments (0)