Crystal for Rubyists

Although Crystal closely resembles ruby in terms of syntax, its a different language, not another ruby implementation. The language is statically typed and compiled making it completely different from ruby.

About crystal: characteristics

  • Ruby-inspired syntax.(Best thing for rubyists)
  • Statically type-checked but without having to specify the type of variables or method arguments.
  • Be able to call C code by writing bindings to it in Crystal.
  • Have compile-time evaluation and generation of code, to avoid boilerplate code.
  • Compiles to efficient native code. LLVM takes cares of all the heavy lifting.
  • Macros

New language: 3 years old

A new language implies:

  • New Syntax
  • New Documentation
  • New Standard Library
  • New Ecosystem
  • New Community

Explanation of each term about new language and basic philosophy about the programming languages.

Explanation of how crystal inherited from ruby

Good Part of Ruby:

  • Beautiful syntax
    class CrystalSyntax < RubySyntax
        AWESOME
    end
    
  • Great Feature
    class CrystalFeature < RubyFeature
    end
    
  • Vibrant community
    class CrystalCommunity
        SMALL: As of now
        FOLLOW: MINASWAN(Matz is nice so we are nice)
    end
    
  • Builtin testing framework(Similar to rspec)
  • Matz supports it

Bad Part:

  • Speed
  • Concurrency support
  • Metaprogramming

Explanation of all features in detail:-

  • Syntax: A very basic HTTP server
    require "http/server"
    server = HTTP::Server.new(8080) do |request|
    HTTP::Response.ok "text/plain", "Hello world, got #{request.path}!"
    end
    puts "Listening on http://0.0.0.0:8080"
    server.listen
    

Wow!! This seems like ruby.

With few differences. What are those? let discuss :

  • Method Definition: we can pass types for arguments saving compiler to figure it out for us.
      def cool_method(argument: String); end
      def cool_method(argument: (int32|Float)); end;
      def cool_method(); end
    
  • Optimatization:
      struct Cool
        property x  # same as attr_accessor in ruby
    
        def initialize(@x)
        end
      end
    

Difference between struct and class: memory allocation

        for struct: on Stack | faster
        for class: on heap | slow compare to struct
  • getter and setter
      getter : attr_reader
      setter : attr_writter
      property : attr_accessor
    
      tuple class for free element:
      user = { 1, "crystal", :user }
      #Tuple(Int32, String, symbol)
    
  • New Data Structure
      "this is crystal string" #String
      ‘C’ #char
  • Compilation:- compiler error introduction
       array = []
    
       array << "hello" #=>  CRASH in crystal
       array << "hello" #=>  WORKS FINE in Ruby
    
       Solution : Specify types of value array can have upfront.
    
       array = [] of String # In crystal
    
  • Concurrency:
    • Threads and process
    • Ruby cannot execute multiple thread at a time because:
    • GVL: One thead locks the whole code and prevent other threads to run
    • Long road to Ruby 3.0 (current stable version of ruby 2.3.0)
    • Crystal’s concurrency support:
      • Channels and Fibers (inspired from Go-lang coroutines)
      • very early stage
  • MACROS
    • Crystal provides a way of doing metaprogramming kind of stuff using macros. But good thing about macro is that they expanded during compile time and not runtime. So there’s no runtime magic.

Ruby Meta Programming:

        ["juice", "shake"].each do |obj|
          define_method "get_#{obj}" do
            puts "got #{obj}"
          end
        end

        Crystal Macros:
          macro define_cool_method(name, content)
            def {{name}}
                  {{content}}
            end
          end

         #This generates:
        def foo
           1
        end
        define_method foo, 1

        foo #=> 1
  • Some other important features
    • C bindings:
    • pointers:
    • Type reflection:
    • Finilize method:
    • Generics:
    • Attribute:

Why developers should use this:

  • Easy to understand. if you know ruby, it’s easy to get started, 90% syntax is same and semantics is about 80% same
  • concurrency abstraction

References: http://crystal-lang.org/docs/

One thought on “Crystal for Rubyists

Leave a comment