Heron's Formula C-extension using the Ruby C API. This repo contains a C-extension for Ruby which contains two methods:
- verifies that a triangle is legitimate (verify_triangle)
- computes the area of a triangle using Heron's formula (heron)
Naturally all work on this was done using my personal Emacs distribution, Aether Emacs.
This was compiled using GCC on x86_64 Mac OS X with Ruby 3.0.2 (tested with 2.6+).
- Pull down the code from the repository.
- Run
ruby extconf.rb
and it will create a Makefile. - Run
make
to generate theheron.bundle
andheron.o
libs. Depending on your architecture, it may generate aheron.so
file.
- Run
irb
- Once in IRB, run
require_relative "./heron.bundle"
and you should seetrue
as output.
To verify a triangle is legitimate, run verify_triangle(a, b, c)
where a, b, and c are numeric values. Floats are
accepted and ints are auto-converted.
irb(main):001:0> require_relative "./heron.bundle"
=> true
irb(main):002:0> verify_triangle(10, 30, 30)
=> true
To compute the area of a triangle, run heron(a, b, c)
where a, b, and c are numeric values. Once again, floats are accepted and ints are auto-converted.
irb(main):001:0> require_relative "./heron.bundle"
=> true
irb(main):002:0> heron(10, 30, 30)
=> 147.90199279785156
The Definitive Guide to Ruby's C API is a great resource for Ruby's C API with lots of information and examples.
C Ruby Extension Playlist is a great collection of videos on YouTube by Sourav Goswami which runs through how to work with various types as well as some examples.