Skip to content

Tutorial about Crystal Language for Complementos Sobre Linguagens de Programação class

Notifications You must be signed in to change notification settings

Dan1m4D/Crystal_Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

CSLP - Crystal Programming Language

Our small presentation and tutorial for the CSLP class of 2023/2024


 

All Contributors PRs Welcome PRs Welcome

Description

This page is dedicated to our one hour tutorial for the Crystal programming language.

Crystal is a multi-paradigm language designed with the intention of merging high-level heavy abstraction languages such as Python and Ruby with low-level performance oriented languages such as C and Rust. The moto "Fast as C, slick as Ruby" was then born.

The syntax is not particularly close to any other language that we learnt in our course, but it is consice and humanly easy to read, being very simillar to Ruby.

Crystal exists since June 2011 and as since recieved multiple overhauls and updates to arrive at its current state.

Crystal supports automatic typing, garbage collection, macros, generic methods/operators and method overloading, as well has a built-in concurrency model for multi-threading applications.

Documentation

Crystal's main documentation is provided inside their website.

Instalation:

Ubuntu, Debian, Fedora and Red Hat

curl -fsSL https://crystal-lang.org/install.sh | sudo bash

Arch

pacman -S crystal shards

NOTE If you don't find your linux distribution iin this file you can check all crystal supported linux distributions in Crystal official website.

Simple code example:

This is a simple file structure example for prototyping:

class Square
    #  Creates a new "Square" instance
    def initialize(@colour = "Green", @length = 1)
        raise "Length must be larger than 0!" if @length <= 0
    end

    #  Returns the current colour
    def getColour
        @colour
    end

    # Returns the current length
    def getLength
        @length
    end

    # Setter for the length variable
    def length=(new_length)
        raise "Length must be larger than 0!" if new_length <= 0
        @length = new_length
    end

    #  Prints the object's atributes to the COUT 
    def printOut
        puts "I'm a #{@colour} square with length #{@length}"
    end
end

x = 2023
puts "This is a prototype for a simple object based Crystal program!"
puts "Var x => #{x}"
puts ""

# Create a new Square instance with default values
default_square = Square.new
default_square.printOut

# Change the length using the setter
default_square.length = 5
default_square.printOut

# Create a new Square instance with custom values
custom_square = Square.new("Blue", 3)
custom_square.printOut

Shards

Shards is the official package manager for the Crystal programming language. It simplifies the process of managing external dependencies and libraries in Crystal projects. With Shards, developers can easily include and use external code libraries in their applications, enhancing functionality and reducing the need to reinvent the wheel.

Shards utilizes a shard.yml file to defin project dependencies, making it straightforward for developers to specify the libraries and versions their project relies on. It also provides commands for fetching and managing dependencies, streamlining the process of integrating third-party code into Crystal projects.

By leveraging Shards, Crystal developers can efficiently build and maintain modular, scalable, and feature-rich applications, taking advantage of a vibrant ecosystem of community-contributed libraries and tools.

#  Execute the file with
crystal prototype.cr

This is a simple shard dependency file, that can be generated with the command:

shards init
name: Example
version: 0.1.0

authors:
    - name <[email protected]>

description: |
    Short description of the project

dependencies:
    amqp-client:
        github: cloudamqp/amqp-client.cr
    pg:
        github: will/crystal-pg
        version: "~> 0.5"

development_dependencies:
    webmock:
        github: manastech/webmock.cr

license: MIT

Our Team ✨

Thanks goes to these wonderful people:

Ramos
Pedro Ramos
107348

💻 ⚠️ 🔧
Madureira
Daniel Madureira
107603

💻🎨📝
Kauati
Rafael Kauati
105925

💻🔀🔧

About

Tutorial about Crystal Language for Complementos Sobre Linguagens de Programação class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published