Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Crystal Clean
Search
Luís Ferreira
January 22, 2015
Technology
2
220
Crystal Clean
An overview of the Crystal programming language, with a lot of comparisons with Ruby.
Luís Ferreira
January 22, 2015
Tweet
Share
More Decks by Luís Ferreira
See All by Luís Ferreira
Winter is coming
zamith
0
670
SI - Module5
zamith
0
66
Design Sprints
zamith
0
140
Continuous learning, teaching and the art of improving yourself
zamith
0
130
Clean Code
zamith
3
190
The best language I have ever learned.
zamith
3
1.6k
Testing Magic
zamith
3
1.5k
Clean Code
zamith
7
1.6k
Other Decks in Technology
See All in Technology
Microsoft Intune アプリのトラブルシューティング
sophiakunii
1
370
株式会社ログラス − エンジニア向け会社説明資料 / Loglass Comapany Deck for Engineer
loglass2019
3
28k
20241108_CS_LLMMT
shigashiyama
0
220
LINEヤフー株式会社における音声言語情報処理AI研究開発@SP/SLP研究会 2024.10.22
lycorptech_jp
PRO
2
260
Team Dynamicsを目指すウイングアーク1stのQAチーム
sadonosake
1
170
データの信頼性を支える仕組みと技術
chanyou0311
3
1.4k
RAGのためのビジネス文書解析技術
eida
3
590
What to do after `laravel new`
mattstauffer
0
130
2024年グライダー曲技世界選手権参加報告/2024 WGAC report
jscseminar
0
140
ZOZOTOWNでの推薦システム活用事例の紹介
f6wbl6
1
430
10分でわかるfreeeのQA
freee
1
3.4k
マイベストのデータ基盤の現在と未来 / mybest-data-infra-asis-tobe
mybestinc
2
1.8k
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
810
Visualization
eitanlees
145
15k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Practical Orchestrator
shlominoach
186
10k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
505
140k
Transcript
CRYSTAL by Luís Zamith
Efficient, Ruby like language FEATURES
Static checks FEATURES
Macros FEATURES
Simple C bindings FEATURES
WARNING! Pre-Alpha
def fib(n)! if n < 3! 1! else! fib(n-1) +
fib(n-2)! end! end! ! fib(35)
RUBY def fib(n)! if n < 3! 1! else! fib(n-1)
+ fib(n-2)! end! end! ! fib(35)
CRYSTAL def fib(n)! if n < 3! 1! else! fib(n-1)
+ fib(n-2)! end! end! ! fib(35)
Calculating ------------------------------------- ruby 1.000 i/100ms crystal 2.000 i/100ms ----------------------------------------------------- ruby
1.268 (± 0.0%) i/s - 13.000 crystal 26.321 (± 3.8%) i/s - 264.000 ! Comparison: crystal: 26.3 i/s ruby: 1.3 i/s - 20.75x slower
RUBY class Email! attr_reader :subject, :date, :from! ! def initialize(subject,
date: "", from: "")! @subject = subject! @date = date! @from = from! end! ! def to_s ! “Date: #{date}! From: #{from}! Subject: #{subject}”! end! end
CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,
options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,
options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,
options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,
options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
Static Checks
RUBY def string_size(str)! puts str.length! end! ! string_size("This is a
string")! string_size(nil) 16 `string_size': undefined method `length' for nil:NilClass (NoMethodError)
CRYSTAL def string_size(str)! puts str.length! end! ! string_size("This is a
string")! string_size(nil) instantiating 'string_size(Nil)' undefined method 'length' for Nil
Special getters
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date Nil assertion failed
CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!
! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date undefined method 'date' for Email (did you mean 'date?'?)
Macros
RUBY class BasicObject! def self.attr_reader(*names)! names.map(&:to_s).each do |name|! class_eval "!
def #{name}! @#{name}! end! "! end! end! end
CRYSTAL class Object! macro getter(*names)! {% for name in names
%}! {% name = name.var if name.is_a?(DeclareVar) %}! ! def {{name.id}}! @{{name.id}}! end! {% end %}! end! end
C bindings
RUBY Not very simple to connect ! Need to write
C ! Need to know Ruby’s C implementation (struct, methods, etc…)
CRYSTAL lib C! fun atoi(str : UInt8*) : Int32! end!
! p C.atoi(“28").class # Int32
CRYSTAL @[Link("readline")]! lib LibReadline! fun readline(prompt : UInt8*) : Pointer(UInt8)!
end! ! line = LibReadline.readline("What's your name?\n")! p String.new(line) # What’s your name?! # zamith! # ”zamith"
Generics
RUBY class Foo! attr_reader :value! ! def initialize(value)! @value =
value! end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97
CRYSTAL class Foo! getter :value! ! def initialize(@value); end! end!
! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new(‘a’)! p foo.value.ord # undefined method 'abs' for Char
CRYSTAL class Foo(T)! getter :value! ! def initialize(@value : T);
end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97
CRYSTAL class Array(T)! include Enumerable! include Comparable(Array)! ! def initialize(size,
value : T)! ! ! . . .! end! end arr = [1, 2]! arr.first.abs! ! arr = [1, '2']! arr.first.abs # error!
Specs
CRYSTAL require "spec"! ! class MyString! def initialize(@string); end! !
def size! @string.size! end! end! ! describe "Repo" do! describe "size" do! it "returns the size of my string" do! MyString.new("Hello").size.should eq 5! end! end! end
Notable Misses
CRYSTAL Single quoted strings ! require_relative ! keyword arguments !
send method_missing ! define_method ! eval
Interesting Things
CRYSTAL RUBY [1,2].map &.to_s.+(“ hello”) [1,2].map do |num| ! !
num.to_s.+(" hello”)! end # [“1 hello", "2 hello"] # [“1 hello", "2 hello"]
CRYSTAL class Hash(K, V)! ...! def self.new(comp = StandardComparator, &block
: (Hash(K, V), K -> V))! new block, comp! end! ! def self.new(default_value : V, comp = StandardComparator)! new(comp) { default_value }! end! ! def self.new(comparator)! new nil, comparator! end! ...! end
CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :
V, comp = StandardComparator)! new(comp) { default_value }! end! ...! end
CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :
V?, comp = StandardComparator)! new(comp) { default_value }! end! ...! end
CRYSTAL - DEPENDENCIES
DEMO