SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Downloaden Sie, um offline zu lesen
The Language Tools
Ruby: Why We Love It
https://github.com/Kelsin/ruby-presentation
Christopher Giroir
November 8th, 2011
Christopher Giroir Ruby: Why We Love It
The Language Tools
1 The Language
Basics
Why We Love It
Gems
2 Tools
Bundler
RVM
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Overview
Inspired by Smalltalk (which I love)
Also draws from Perl, Eiffel, Ada and LISP
Includes a REPL
Built for developers as a language they would love to use
Dynamic, strict, reflective, object oriented
Everything is an expression (even statements)
Everything is executed imperatively (even declarations)
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Object Oriented
Everything is an object
Single Inheritance
Modules can be mixed in
Dynamic Dispatch
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Simple Code
1 5.times { print "Hello" }
This outputs:
1 Hello
2 Hello
3 Hello
4 Hello
5 Hello
6 => 5
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Types
1 # Strings
2 s = ’Testing’
3
4 # Interpreted Strings
5 t = "Double #{str}"
6
7 # Symbols
8 sym = :chris
9
10 # Arrays
11 a = [1,2,3]
12
13 # Hashes
14 h = { :key => ’value’, :chris => ’awesome’ }
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Classes
1 class Box
2 def initialize(w,h,d)
3 @width = w
4 @height = h
5 @depth = d
6 end
7
8 def volume
9 @width * @height * @depth
10 end
11 end
12
13 box = Box.new(2,2,2)
14 box.volume # => 8
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Simple Inheritance
1 class JackInTheBox < Box
2 def initialize(msg)
3 @msg = msg
4 super(3,3,3)
5 end
6
7 def open
8 puts @msg
9 end
10 end
11
12 jbox = JackInTheBox.new(’Surprise!’)
13 jbox.volume # => 27
14 jbox.open # prints ’Surprise!’
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Control
1 while true == false
2 if var == 5
3 break
4 end
5
6 begin
7 var - 1
8 end while var < 4
9
10 next if var == 6
11 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Blocks
1 [1,2,3].each { |n| puts n }
This outputs:
1 1
2 2
3 3
4 => [1,2,3]
Christopher Giroir Ruby: Why We Love It
The Language Tools
Basics
Block Syntax
1 5.upto(10) { |n| puts n }
This is exactly the same as the following:
1 5.upto(10) do |n|
2 puts n
3 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Why We Love It
Attribute Methods
1 class Person
2 def name
3 @name
4 end
5 def social=(s)
6 @social = s
7 end
8 def age
9 @age
10 end
11 def age=(a)
12 @age = a
13 end
14 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Why We Love It
The Easy Way
1 class Person
2 attr_reader :name
3 attr_writer :social
4 attr_accessor :age
5 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Why We Love It
The Easy Way Explained
1 class Person
2 attr_reader :name
3 attr_writer :social
4 attr_accessor :age
5 end
Ruby syntax allows method calls without ()
Result is clean and looks like a language feature
Christopher Giroir Ruby: Why We Love It
The Language Tools
Why We Love It
We can implement this ourselves
Untested code, please do not copy:
1 class Object
2 def self.attr_reader(var)
3 class_eval <<-METHODS
4 def #{var}
5 @#{var}
6 end
7 METHODS
8 end
9 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Why We Love It
Why Blocks
1 (map (lambda (n)
2 (+ n 5))
3 ’(1 2 3))
Becomes:
1 [1,2,3].map do |n|
2 n + 5
3 end
Results in:
1 => [6,7,8]
Christopher Giroir Ruby: Why We Love It
The Language Tools
Gems
Modules
1 module Voice
2 def say(msg)
3 puts msg
4 end
5 end
6
7 class Person
8 include Voice
9 end
10
11 p = Person.new
12 p.say(’Hello’) # prints ’Hello’
Christopher Giroir Ruby: Why We Love It
The Language Tools
Gems
Using Gems
Require loads in files
1 require ’saver’ # pulls in ’saver.rb’
Gems allow us to not deal with paths
1 require ’rubygems’
2 require ’saver’
3
4 class Item
5 include Saver
6 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Gems
Writing Gems
1 Gem::Specification.new do |s|
2 s.name = "saver"
3 s.version = Saver::VERSION
4 s.authors = ["Christopher Giroir"]
5 s.email = ["kelsin@valefor.com"]
6 s.homepage = "http://kelsin.github.com/saver/"
7
8 s.files = ‘git ls-files‘.split("n")
9 s.require_paths = ["lib"]
10
11 s.add_dependency ’activesupport’, ’~> 3.0.0’
12 s.add_dependency ’mongo_mapper’
13 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Bundler
Why Bundler?
Many projects (i.e. rails apps) are not gems themselves
They do have gem dependencies
Easy way to install and keep track of these dependencies
Making sure ONLY the proper gems are used
Christopher Giroir Ruby: Why We Love It
The Language Tools
Bundler
The Gemfile
1 source ’http://tools1.savewave.com/rubygems’
2 source ’http://rubygems.org’
3
4 gem ’rails’, ’3.0.7’
5
6 gem ’sw-model’, ’0.13.0’
7
8 group :development, :test do
9 gem "rspec"
10 end
Christopher Giroir Ruby: Why We Love It
The Language Tools
Bundler
Using Bundler
1 # Install the gems from the Gemfile
2 bundle install
3
4 # Update gems to new versions
5 bundle update
6
7 # Execute command with proper gems
8 bundle exec rake spec
In your ruby code
1 require "rubygems"
2 require "bundler/setup"
3 require "saver"
Christopher Giroir Ruby: Why We Love It
The Language Tools
Bundler
Gemfile.lock
When you initially install versions are saved to Gemfile.lock
After they are only updated on bundle update
SHOULD be checked into version control
Protects from version updates
Christopher Giroir Ruby: Why We Love It
The Language Tools
RVM
Why RVM?
Different projects might use different versions of rails
Different projects might use different ruby interpreters
Ruby
JRuby
Rubinus
While bundler helps, complete gem isolation is better!
It’s nice to keep your system ruby separate and not update it
Christopher Giroir Ruby: Why We Love It
The Language Tools
RVM
Using RVM
1 # Install the default 1.9.2 ruby interpretor
2 rvm install 1.9.2
3
4 # Switch to using 1.9.2
5 rvm use 1.9.2
6
7 # List installed rubies
8 rvm list
Christopher Giroir Ruby: Why We Love It
The Language Tools
RVM
RVM Gemsets
1 # Create a new gemset
2 rvm gemset create savingstar-web
3
4 # List gemsets
5 rvm gemset list
6
7 # Switch to a ruby and gemset together
8 rvm use 1.9.2@savingstar-web
Christopher Giroir Ruby: Why We Love It
The Language Tools
RVM
.rvmrc
A .rvmrc file per project allows you to say which ruby and
gemset to use
Should be in source control. Helps RVM users out, ignored for
others
It’s a shell script that’s executed everytime you cd (very
unsafe)
Makes life very easy however!
1 rvm use 1.9.2@saveingstar-web --create
Christopher Giroir Ruby: Why We Love It

Weitere ähnliche Inhalte

Ähnlich wie Ruby Presentation - Handout

Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovMichael Kimathi
 
Ruby Presentation
Ruby Presentation Ruby Presentation
Ruby Presentation platico_dev
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - IntroductionKwangshin Oh
 
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and SpeedSalesforce Marketing Cloud
 
introduction-to-ruby
introduction-to-rubyintroduction-to-ruby
introduction-to-rubyAdemar Tutor
 
Ruby Rails Overview
Ruby Rails OverviewRuby Rails Overview
Ruby Rails OverviewNetguru
 
How to create a programming language
How to create a programming languageHow to create a programming language
How to create a programming languageRobert Mamore
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHiroshi SHIBATA
 
Get your ass to 1.9
Get your ass to 1.9Get your ass to 1.9
Get your ass to 1.9Nic Benders
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9sagaroceanic11
 

Ähnlich wie Ruby Presentation - Handout (20)

Ruby Presentation - Beamer
Ruby Presentation - BeamerRuby Presentation - Beamer
Ruby Presentation - Beamer
 
Ruby Presentation - Article
Ruby Presentation - ArticleRuby Presentation - Article
Ruby Presentation - Article
 
Perl 101
Perl 101Perl 101
Perl 101
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar BatsovRuby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
Ruby 4.0 To Infinity and Beyond at Ruby Conference Kenya 2017 by Bozhidar Batsov
 
Ruby 101
Ruby 101Ruby 101
Ruby 101
 
Ruby Presentation
Ruby Presentation Ruby Presentation
Ruby Presentation
 
Ruby
RubyRuby
Ruby
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - Introduction
 
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed#CNX14 - Using Ruby for Reliability, Consistency, and Speed
#CNX14 - Using Ruby for Reliability, Consistency, and Speed
 
introduction-to-ruby
introduction-to-rubyintroduction-to-ruby
introduction-to-ruby
 
Ruby Rails Overview
Ruby Rails OverviewRuby Rails Overview
Ruby Rails Overview
 
How to create a programming language
How to create a programming languageHow to create a programming language
How to create a programming language
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
Get your ass to 1.9
Get your ass to 1.9Get your ass to 1.9
Get your ass to 1.9
 
IJTC%202009%20JRuby
IJTC%202009%20JRubyIJTC%202009%20JRuby
IJTC%202009%20JRuby
 
IJTC%202009%20JRuby
IJTC%202009%20JRubyIJTC%202009%20JRuby
IJTC%202009%20JRuby
 
Why Ruby?
Why Ruby? Why Ruby?
Why Ruby?
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 
Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9Rubyforjavaprogrammers 1210167973516759-9
Rubyforjavaprogrammers 1210167973516759-9
 

Kürzlich hochgeladen

UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 

Kürzlich hochgeladen (20)

UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 

Ruby Presentation - Handout

  • 1. The Language Tools Ruby: Why We Love It https://github.com/Kelsin/ruby-presentation Christopher Giroir November 8th, 2011 Christopher Giroir Ruby: Why We Love It The Language Tools 1 The Language Basics Why We Love It Gems 2 Tools Bundler RVM Christopher Giroir Ruby: Why We Love It The Language Tools Basics Overview Inspired by Smalltalk (which I love) Also draws from Perl, Eiffel, Ada and LISP Includes a REPL Built for developers as a language they would love to use Dynamic, strict, reflective, object oriented Everything is an expression (even statements) Everything is executed imperatively (even declarations) Christopher Giroir Ruby: Why We Love It The Language Tools Basics Object Oriented Everything is an object Single Inheritance Modules can be mixed in Dynamic Dispatch Christopher Giroir Ruby: Why We Love It
  • 2. The Language Tools Basics Simple Code 1 5.times { print "Hello" } This outputs: 1 Hello 2 Hello 3 Hello 4 Hello 5 Hello 6 => 5 Christopher Giroir Ruby: Why We Love It The Language Tools Basics Types 1 # Strings 2 s = ’Testing’ 3 4 # Interpreted Strings 5 t = "Double #{str}" 6 7 # Symbols 8 sym = :chris 9 10 # Arrays 11 a = [1,2,3] 12 13 # Hashes 14 h = { :key => ’value’, :chris => ’awesome’ } Christopher Giroir Ruby: Why We Love It The Language Tools Basics Classes 1 class Box 2 def initialize(w,h,d) 3 @width = w 4 @height = h 5 @depth = d 6 end 7 8 def volume 9 @width * @height * @depth 10 end 11 end 12 13 box = Box.new(2,2,2) 14 box.volume # => 8 Christopher Giroir Ruby: Why We Love It The Language Tools Basics Simple Inheritance 1 class JackInTheBox < Box 2 def initialize(msg) 3 @msg = msg 4 super(3,3,3) 5 end 6 7 def open 8 puts @msg 9 end 10 end 11 12 jbox = JackInTheBox.new(’Surprise!’) 13 jbox.volume # => 27 14 jbox.open # prints ’Surprise!’ Christopher Giroir Ruby: Why We Love It
  • 3. The Language Tools Basics Control 1 while true == false 2 if var == 5 3 break 4 end 5 6 begin 7 var - 1 8 end while var < 4 9 10 next if var == 6 11 end Christopher Giroir Ruby: Why We Love It The Language Tools Basics Blocks 1 [1,2,3].each { |n| puts n } This outputs: 1 1 2 2 3 3 4 => [1,2,3] Christopher Giroir Ruby: Why We Love It The Language Tools Basics Block Syntax 1 5.upto(10) { |n| puts n } This is exactly the same as the following: 1 5.upto(10) do |n| 2 puts n 3 end Christopher Giroir Ruby: Why We Love It The Language Tools Why We Love It Attribute Methods 1 class Person 2 def name 3 @name 4 end 5 def social=(s) 6 @social = s 7 end 8 def age 9 @age 10 end 11 def age=(a) 12 @age = a 13 end 14 end Christopher Giroir Ruby: Why We Love It
  • 4. The Language Tools Why We Love It The Easy Way 1 class Person 2 attr_reader :name 3 attr_writer :social 4 attr_accessor :age 5 end Christopher Giroir Ruby: Why We Love It The Language Tools Why We Love It The Easy Way Explained 1 class Person 2 attr_reader :name 3 attr_writer :social 4 attr_accessor :age 5 end Ruby syntax allows method calls without () Result is clean and looks like a language feature Christopher Giroir Ruby: Why We Love It The Language Tools Why We Love It We can implement this ourselves Untested code, please do not copy: 1 class Object 2 def self.attr_reader(var) 3 class_eval <<-METHODS 4 def #{var} 5 @#{var} 6 end 7 METHODS 8 end 9 end Christopher Giroir Ruby: Why We Love It The Language Tools Why We Love It Why Blocks 1 (map (lambda (n) 2 (+ n 5)) 3 ’(1 2 3)) Becomes: 1 [1,2,3].map do |n| 2 n + 5 3 end Results in: 1 => [6,7,8] Christopher Giroir Ruby: Why We Love It
  • 5. The Language Tools Gems Modules 1 module Voice 2 def say(msg) 3 puts msg 4 end 5 end 6 7 class Person 8 include Voice 9 end 10 11 p = Person.new 12 p.say(’Hello’) # prints ’Hello’ Christopher Giroir Ruby: Why We Love It The Language Tools Gems Using Gems Require loads in files 1 require ’saver’ # pulls in ’saver.rb’ Gems allow us to not deal with paths 1 require ’rubygems’ 2 require ’saver’ 3 4 class Item 5 include Saver 6 end Christopher Giroir Ruby: Why We Love It The Language Tools Gems Writing Gems 1 Gem::Specification.new do |s| 2 s.name = "saver" 3 s.version = Saver::VERSION 4 s.authors = ["Christopher Giroir"] 5 s.email = ["kelsin@valefor.com"] 6 s.homepage = "http://kelsin.github.com/saver/" 7 8 s.files = ‘git ls-files‘.split("n") 9 s.require_paths = ["lib"] 10 11 s.add_dependency ’activesupport’, ’~> 3.0.0’ 12 s.add_dependency ’mongo_mapper’ 13 end Christopher Giroir Ruby: Why We Love It The Language Tools Bundler Why Bundler? Many projects (i.e. rails apps) are not gems themselves They do have gem dependencies Easy way to install and keep track of these dependencies Making sure ONLY the proper gems are used Christopher Giroir Ruby: Why We Love It
  • 6. The Language Tools Bundler The Gemfile 1 source ’http://tools1.savewave.com/rubygems’ 2 source ’http://rubygems.org’ 3 4 gem ’rails’, ’3.0.7’ 5 6 gem ’sw-model’, ’0.13.0’ 7 8 group :development, :test do 9 gem "rspec" 10 end Christopher Giroir Ruby: Why We Love It The Language Tools Bundler Using Bundler 1 # Install the gems from the Gemfile 2 bundle install 3 4 # Update gems to new versions 5 bundle update 6 7 # Execute command with proper gems 8 bundle exec rake spec In your ruby code 1 require "rubygems" 2 require "bundler/setup" 3 require "saver" Christopher Giroir Ruby: Why We Love It The Language Tools Bundler Gemfile.lock When you initially install versions are saved to Gemfile.lock After they are only updated on bundle update SHOULD be checked into version control Protects from version updates Christopher Giroir Ruby: Why We Love It The Language Tools RVM Why RVM? Different projects might use different versions of rails Different projects might use different ruby interpreters Ruby JRuby Rubinus While bundler helps, complete gem isolation is better! It’s nice to keep your system ruby separate and not update it Christopher Giroir Ruby: Why We Love It
  • 7. The Language Tools RVM Using RVM 1 # Install the default 1.9.2 ruby interpretor 2 rvm install 1.9.2 3 4 # Switch to using 1.9.2 5 rvm use 1.9.2 6 7 # List installed rubies 8 rvm list Christopher Giroir Ruby: Why We Love It The Language Tools RVM RVM Gemsets 1 # Create a new gemset 2 rvm gemset create savingstar-web 3 4 # List gemsets 5 rvm gemset list 6 7 # Switch to a ruby and gemset together 8 rvm use 1.9.2@savingstar-web Christopher Giroir Ruby: Why We Love It The Language Tools RVM .rvmrc A .rvmrc file per project allows you to say which ruby and gemset to use Should be in source control. Helps RVM users out, ignored for others It’s a shell script that’s executed everytime you cd (very unsafe) Makes life very easy however! 1 rvm use 1.9.2@saveingstar-web --create Christopher Giroir Ruby: Why We Love It