SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
CRYSTAL - STATICALLY TYPED RUBY
Vagmi Mudumbai - @vagmi (github / twitter)
τarka λabs
τarka λabs@vagmi
puts "hello RubyConfIndia"
τarka λabs@vagmi
Juan WajnermanAry Borenszweig
http://manas.com.ar/
τarka λabs@vagmi
Apache 2.0 License
τarka λabs@vagmi
‣Inspired by Ruby
‣Statically typed
‣Garbage Collected
‣Compiles with LLVM
‣More performant than Ruby
τarka λabs@vagmi
HTTP
JSON
YAML
Fiber
Channel
Spec
OpenSSL
Enumerable
Markdown
OAuth / OAuth2
XML
Web Sockets
τarka λabs@vagmi
τarka λabs@vagmi
⍺
τarka λabs@vagmi
Demo
τarka λabs@vagmi
puts "crystal" + "programming"
puts "1+1 = #{1 + 1}"
puts "7.0/3.0 = #{7.0 / 3.0}"
puts true && false
puts true || false
puts !true
τarka λabs@vagmi
a = "RubyConf India 2016"
puts a # => "RubyConf India 2016"
puts typeof(a) # => String
τarka λabs@vagmi
a = 42
puts a # => 42
puts typeof(a) # => Int32
τarka λabs@vagmi
y = uninitialized Int32
y = 42
puts y
# y = "@vagmi"
# => Error in ./variables/variables.cr:15:
# type must be Int32, not (String | Int32)
τarka λabs@vagmi
z = uninitialized (Int32 | String)
puts z # segfault
# Invalid memory access (signal 11) at address 0x7fff571cb200
τarka λabs@vagmi
a = 20
if rand(100) < 50
a = “@vagmi"
end
puts typeof(a) # => (String | Int32)
τarka λabs@vagmi
tuple = {1, 2.5, 'a'}
puts tuple[0] # => 1
puts typeof(tuple) #=> {Int32, Float64, Char}
a = Tuple.new(2016, “rubyconf india", 'x')
puts a # => {2016, “rubyconf india", 'x'}
puts typeof(a) #=> {Int32, String, Char}
τarka λabs@vagmi
a = {} of String => Int32
a["year"] = 2016
puts a
b = Hash(Int32 | Char, Int32) {3 => 4}
b[1] = 2
b['a'] = 9
puts b # => {3 => 4, 1 => 2, 'a' => 9}
puts b.size # => 3
τarka λabs@vagmi
b.delete('a')
puts b # => {3 => 4, 1 => 2}
p b[true] # runtime error
# should this not be a compile error?
# => Missing hash key: true (KeyError)
p b["bar"]? # nil
τarka λabs@vagmi
if(result)
puts result + 2
end
result = b[1]? + 2
# Error in hash.cr:5: undefined method '+' for Nil
# (compile-time type is Int32?)
τarka λabs@vagmi
def greet(name)
"Hello #{name}"
end
puts greet("RubyConf") # => Hello RubyConf
τarka λabs@vagmi
class String
def greet
"Hello #{self}"
end
end
"RubyConf".greet # => Hello RubyConf
τarka λabs@vagmi
def add2(num)
2 + num
end
puts add2(40) # => 42
puts add2("forty")
# in ./def/funs.cr:17: no overload matches
# 'Int32#+' with type String
def add2(other : String)
"#{other} two"
end
puts add2("forty") # => forty two
τarka λabs@vagmi
abstract class Foo
abstract def foo
def greet
"hello #{@name}"
end
end
class Bar < Foo
def initialize(@name)
end
def foo
1
end
end
a = Bar.new("Bar")
puts a.foo # => 1
puts a.greet # => Hello Bar
class Baz < Foo
def foo
2
end
end
b = Baz.new
puts b.foo # => 2
p b.greet # => "Hello "
τarka λabs@vagmi
- class Object (4 bytes)
+- class Reference (4 bytes)
+- class Foo (16 bytes)
. @name : String? (8 bytes)
+- class Baz (16 bytes)
+- class Bar (16 bytes)
crystal tool hierarchy -e Foo abstract/abstract.cr
τarka λabs@vagmi
struct Entry
property :title, :score, :num_comments
def initialize(@title, @score, @num_comments)
end
def to_json(io)
{
title: title,
score: score,
num_comments: num_comments
}.to_json(io)
end
end
τarka λabs@vagmi
- class Object (4 bytes)
+- struct Value (0 bytes)
| +- struct Struct (0 bytes)
| +- struct Entry (16 bytes)
| | @title : String (8 bytes)
| | @score : Int32 (4 bytes)
| | @num_comments : Int32 (4 bytes)
crystal tool hierarchy -e Entry src/hello-kemal.cr
τarka λabs@vagmi
macro property(*names)
getter {{*names}}
setter {{*names}}
end
τarka λabs@vagmi
macro getter(*names)
{% for name in names %}
{% if name.is_a?(TypeDeclaration) %}
@{{name.id}}
{% name = name.var %}
{% end %}
def {{name.id}}
@{{name.id}}
end
{% end %}
end
τarka λabs@vagmi
macro setter(*names)
{% for name in names %}
{% if name.is_a?(TypeDeclaration) %}
@{{name}}
def {{name.var.id}}=(@{{name.var.id}} :
{{name.type}})
end
{% else %}
def {{name.id}}=(@{{name.id}})
end
{% end %}
{% end %}
end
τarka λabs@vagmi
def make_request
url = "https://www.reddit.com/hot.json"
channel = Channel(String).new
spawn do
HTTP::Client.get(url) do |response|
if(resp_body = response.body_io.gets)
$cache = resp_body
channel.send resp_body
end
end
end
channel
end
τarka λabs@vagmi
get '/feed' do |env|
channel = make_request
entries = JSON.parse(channel.receive)["data"]
["children"].as_a
feed = entries.map do |e|
Entry.from_json(e)
end
env.response.content_type = "application/json"
feed.to_json
end
τarka λabs@vagmi
Crystal Language - http://crystal-lang.org/
Support Crystal - https://salt.bountysource.com/teams/crystal-lang
Crystal Shards - https://crystalshards.herokuapp.com/
Awesome Crystal - http://awesome-crystal.com/
τarka λabs@vagmi
THANKS
@vagmi / @tarkalabs
github / twitter

Contenu connexe

Tendances

Happy Programming with CoffeeScript
Happy Programming with CoffeeScriptHappy Programming with CoffeeScript
Happy Programming with CoffeeScript
Eddie Kao
 

Tendances (10)

End to-End CoffeeScript
End to-End CoffeeScriptEnd to-End CoffeeScript
End to-End CoffeeScript
 
Advanced I/O in browser
Advanced I/O in browserAdvanced I/O in browser
Advanced I/O in browser
 
Marcelo Camargo - Let's dive into Babel: how everything works
Marcelo Camargo - Let's dive into Babel: how everything worksMarcelo Camargo - Let's dive into Babel: how everything works
Marcelo Camargo - Let's dive into Babel: how everything works
 
scala-gopher: async implementation of CSP for scala
scala-gopher:  async implementation of CSP  for  scalascala-gopher:  async implementation of CSP  for  scala
scala-gopher: async implementation of CSP for scala
 
Plumbin Pipelines - A Gulp.js workshop
Plumbin Pipelines - A Gulp.js workshopPlumbin Pipelines - A Gulp.js workshop
Plumbin Pipelines - A Gulp.js workshop
 
Happy Programming with CoffeeScript
Happy Programming with CoffeeScriptHappy Programming with CoffeeScript
Happy Programming with CoffeeScript
 
Advanced JavaScript build pipelines using Gulp.js
Advanced JavaScript build pipelines using Gulp.jsAdvanced JavaScript build pipelines using Gulp.js
Advanced JavaScript build pipelines using Gulp.js
 
Telegram bots
Telegram botsTelegram bots
Telegram bots
 
Ruby.new @ VilniusRB
Ruby.new @ VilniusRBRuby.new @ VilniusRB
Ruby.new @ VilniusRB
 
Csp scala wixmeetup2016
Csp scala wixmeetup2016Csp scala wixmeetup2016
Csp scala wixmeetup2016
 

Similaire à Crystal - Statically Typed Ruby

Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
Wen-Tien Chang
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
Wen-Tien Chang
 

Similaire à Crystal - Statically Typed Ruby (20)

Rubyistを誘うScalaの世界 2.0
Rubyistを誘うScalaの世界 2.0Rubyistを誘うScalaの世界 2.0
Rubyistを誘うScalaの世界 2.0
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
An introduction to Ruby
An introduction to RubyAn introduction to Ruby
An introduction to Ruby
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Hacking with ruby2ruby
Hacking with ruby2rubyHacking with ruby2ruby
Hacking with ruby2ruby
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
 
Introducing Ruby
Introducing RubyIntroducing Ruby
Introducing Ruby
 
JSON and the APInauts
JSON and the APInautsJSON and the APInauts
JSON and the APInauts
 
4 JVM Web Frameworks
4 JVM Web Frameworks4 JVM Web Frameworks
4 JVM Web Frameworks
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
The Enterprise Strikes Back
The Enterprise Strikes BackThe Enterprise Strikes Back
The Enterprise Strikes Back
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
Learning From Ruby (Yapc Asia)
Learning From Ruby (Yapc Asia)Learning From Ruby (Yapc Asia)
Learning From Ruby (Yapc Asia)
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
 
Quick Intro To JRuby
Quick Intro To JRubyQuick Intro To JRuby
Quick Intro To JRuby
 
A Brief Intro to Scala
A Brief Intro to ScalaA Brief Intro to Scala
A Brief Intro to Scala
 
Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)Concurrent programming with Celluloid (MWRC 2012)
Concurrent programming with Celluloid (MWRC 2012)
 
Ruby
RubyRuby
Ruby
 
TDC 2012 - Patterns e Anti-Patterns em Ruby
TDC 2012 - Patterns e Anti-Patterns em RubyTDC 2012 - Patterns e Anti-Patterns em Ruby
TDC 2012 - Patterns e Anti-Patterns em Ruby
 
Jekyll - Liquid for noobs
Jekyll - Liquid for noobsJekyll - Liquid for noobs
Jekyll - Liquid for noobs
 

Plus de Vagmi Mudumbai

Building Single Page Apps with React.JS
Building Single Page Apps with React.JSBuilding Single Page Apps with React.JS
Building Single Page Apps with React.JS
Vagmi Mudumbai
 

Plus de Vagmi Mudumbai (11)

Bitcoin a developer's perspective
Bitcoin a developer's perspectiveBitcoin a developer's perspective
Bitcoin a developer's perspective
 
Purely functional UIs
Purely functional UIsPurely functional UIs
Purely functional UIs
 
Pragmatic Functional Programming in the JS land with Clojurescript and Om
Pragmatic Functional Programming in the JS land with Clojurescript and OmPragmatic Functional Programming in the JS land with Clojurescript and Om
Pragmatic Functional Programming in the JS land with Clojurescript and Om
 
Building Single Page Apps with React.JS
Building Single Page Apps with React.JSBuilding Single Page Apps with React.JS
Building Single Page Apps with React.JS
 
JSFoo 2014 - Building beautiful apps with Clojurescript
JSFoo 2014 - Building beautiful apps with ClojurescriptJSFoo 2014 - Building beautiful apps with Clojurescript
JSFoo 2014 - Building beautiful apps with Clojurescript
 
Real Time Analytics with Cassandra
Real Time Analytics with CassandraReal Time Analytics with Cassandra
Real Time Analytics with Cassandra
 
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
 
Github - Down the Rabbit Hole
Github  - Down the Rabbit HoleGithub  - Down the Rabbit Hole
Github - Down the Rabbit Hole
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
MongoDB - Introduction
MongoDB - IntroductionMongoDB - Introduction
MongoDB - Introduction
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Crystal - Statically Typed Ruby