SlideShare a Scribd company logo
Seven Languages in Seven Days: Ruby
            prepared by @zachleat
Seven Languages in Seven Days: Ruby
Creator
Yukihiro Matsumoto
“Matz”
Influences
Lisp, Smalltalk, Perl
Trade-offs
    Simplicity for Safety
Productivity for Performance
irb
Ruby’s Interactive Console
Syntax
WHEN U DECLARE VARS




U DECLARE WEAKNESS
                 Courage Wolf
RUBY CODE ROI




ALWAYS RETURN
  SOMETHING     Business Cat
Y U NO EVALUTE STRING?




   SINGLE QUOTES!!
   puts “hello, #{language}”
                               Y U NO?
>> puts ‘literal string’
literal string

>> subject = ‘world’
=> “world”

>> puts “hello, #{subject}”
hello, world




                       Y U NO?
HUMANS ARE CARBON




 RUBY IS OBJECTS
                   Philosoraptor
>> 4.class
=> FixNum

>> 4.methods
=> [“inspect”, “%”, “<<“, ...

>> false.class
=> FalseClass




                           Philosoraptor
MY CONDITIONALS



ALWAYS SUCCESSFUL
               Success Kid
>> x = 4
=> 4

>> puts ‘True!!’ if x == 4
True!!
=> nil

>> puts ‘True!!’ unless x == 4
=> nil

>> puts ‘True!!’ if not true
=> nil

>> puts ‘True!!’ if !true
=> nil
                               Success Kid
# Everything but nil and false
# evaluate to true. 0 is true!
>> puts “This is true” if 0
This is true
=> nil

# and, &&
# or, ||

# &, | are the non-short circuit
# equivalents




                             Success Kid
THE BEST LOOPS




ITERATE OVER THIRST
              The Most Interesting Man in the World
>>   x = x + 1 while x < 10
=>   nil
>>   x
=>   10

>>   x = x - 1 until x == 1
=>   nil
>>   x
=>   1




                      The Most Interesting Man in the World
KEYBOARD CAT
HAS NOTHING ON




 DUCK TYPINGTechnologically Impaired Duck
>> 4 + ‘four’
TypeError: String can’t be coerced
into Fixnum

# Strongly typed
# Dynamic: Checked at run time

>> a = [‘100’, 100.0]
=> [‘100’, 100.0]
>> while i < 2
>>   puts a[i].to_i
>>   i += 1
>> end
100
100
                        Technologically Impaired Duck
FFFFFUUUUUUUUUU




UUUUUUUNCTIONS
              FFFUUUUUU
>> def tell_the_truth
>>     true
>> end

# Last expression is return value




                        Technologically Impaired Duck
ARRRRRRRAYS




AND HASHES
>> animals = [‘lions’, ‘tigers’]
=> [‘lions’, ‘tigers’]

>> numbers = {1 => ‘one’, 2 =>
‘two’}
=> {1=>”one”, 2=>”two”}

# Symbols
>>   ‘string’.object_id
=>   3092010
>>   ‘string’.object_id
=>   3089690
>>   :string.object_id
=>   69618
>>   :string.object_id
=>   69618
>> def winning(options = {})
>>   if(options[:profession] == :gambler)
>>     true
>>   else
>>     false
>>   end
>> end
=> nil

>> winning
=> false

# {} optional for last parameter
>> winning(:profession => :lawyer)
=> true
YO DAWG I HEARD YOU
  LIKED CODE BLOCKS



SO YOU COULD RUN CODE
     IN YOUR CODE
>> 3.times { puts ‘hi’ }
hi
hi
hi

>> animals = [‘lions’, ‘tigers’]
>> animals.each {|a| puts a}
lions
tigers

# Blocks can be passed as
parameters
>> def pass_block(&block)
>> end
>> pass_block { puts ‘hi’ }
Classes
class MyClass
  def initialize(name)
    @name = name # instance var
    @@other = ‘’ # class var
  end

  def name
    return @name
  end

  # methods that check end in ?
end

my_class = MyClass.new(‘Name’)
my_class.name # returns ‘Name’
Modules
module MyModule
  def name
    return @name
  end
end

class MyClass
  include MyModule

  def initialize(name)
    @name = name
  end
end

my_class = MyClass.new(‘Name’)
my_class.name # returns ‘Name’
Enumerable
# Implements each method




Comparable
# Implements <=> (spaceship) method
Open Classes
# First invocation defines
# Second invocation modifies

class NilClass
  def blank?
    true
  end
end

class String
  def blank?
    self.size == 0
  end
end

[‘’, ‘person’, nil].each {|a| puts a unless a.blank?}
# outputs person
method_missing

More Related Content

PDF
Ruby 101
PPT
eJADA web development the Ruby way
PDF
Girl Geek Dinners - Clojure 101
ODP
Python an-intro - odp
PDF
#safaDojo - Coding Dojo Go lang
PPTX
Presentation of WAQE by Jortilles on PCM '15
PDF
Async: ways to store state
Ruby 101
eJADA web development the Ruby way
Girl Geek Dinners - Clojure 101
Python an-intro - odp
#safaDojo - Coding Dojo Go lang
Presentation of WAQE by Jortilles on PCM '15
Async: ways to store state

Similar to Seven Languages in Seven Days: Ruby (20)

PDF
What I learned from Seven Languages in Seven Weeks (IPRUG)
PDF
7li7w devcon5
KEY
PDF
Beautiful python - PyLadies
PDF
Python WATs: Uncovering Odd Behavior
KEY
Desarrollando aplicaciones web en minutos
PDF
Ruby 程式語言入門導覽
PDF
RedDot Ruby Conf 2014 - Dark side of ruby
KEY
Introduction to Ruby
PDF
Python! An Introduction
PDF
Ruby Intro {spection}
KEY
Refactor like a boss
PDF
Learn 90% of Python in 90 Minutes
KEY
An introduction to Ruby
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
PDF
Values
PPTX
Python Workshop
PPTX
Python chapter 2
PPTX
python chapter 1
What I learned from Seven Languages in Seven Weeks (IPRUG)
7li7w devcon5
Beautiful python - PyLadies
Python WATs: Uncovering Odd Behavior
Desarrollando aplicaciones web en minutos
Ruby 程式語言入門導覽
RedDot Ruby Conf 2014 - Dark side of ruby
Introduction to Ruby
Python! An Introduction
Ruby Intro {spection}
Refactor like a boss
Learn 90% of Python in 90 Minutes
An introduction to Ruby
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
Values
Python Workshop
Python chapter 2
python chapter 1
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Digital-Transformation-Roadmap-for-Companies.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Big Data Technologies - Introduction.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Empathic Computing: Creating Shared Understanding
Dropbox Q2 2025 Financial Results & Investor Presentation
Building Integrated photovoltaic BIPV_UPV.pdf
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Review of recent advances in non-invasive hemoglobin estimation
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Machine learning based COVID-19 study performance prediction
Understanding_Digital_Forensics_Presentation.pptx
Ad

Seven Languages in Seven Days: Ruby

  • 1. Seven Languages in Seven Days: Ruby prepared by @zachleat
  • 5. Trade-offs Simplicity for Safety Productivity for Performance
  • 8. WHEN U DECLARE VARS U DECLARE WEAKNESS Courage Wolf
  • 9. RUBY CODE ROI ALWAYS RETURN SOMETHING Business Cat
  • 10. Y U NO EVALUTE STRING? SINGLE QUOTES!! puts “hello, #{language}” Y U NO?
  • 11. >> puts ‘literal string’ literal string >> subject = ‘world’ => “world” >> puts “hello, #{subject}” hello, world Y U NO?
  • 12. HUMANS ARE CARBON RUBY IS OBJECTS Philosoraptor
  • 13. >> 4.class => FixNum >> 4.methods => [“inspect”, “%”, “<<“, ... >> false.class => FalseClass Philosoraptor
  • 15. >> x = 4 => 4 >> puts ‘True!!’ if x == 4 True!! => nil >> puts ‘True!!’ unless x == 4 => nil >> puts ‘True!!’ if not true => nil >> puts ‘True!!’ if !true => nil Success Kid
  • 16. # Everything but nil and false # evaluate to true. 0 is true! >> puts “This is true” if 0 This is true => nil # and, && # or, || # &, | are the non-short circuit # equivalents Success Kid
  • 17. THE BEST LOOPS ITERATE OVER THIRST The Most Interesting Man in the World
  • 18. >> x = x + 1 while x < 10 => nil >> x => 10 >> x = x - 1 until x == 1 => nil >> x => 1 The Most Interesting Man in the World
  • 19. KEYBOARD CAT HAS NOTHING ON DUCK TYPINGTechnologically Impaired Duck
  • 20. >> 4 + ‘four’ TypeError: String can’t be coerced into Fixnum # Strongly typed # Dynamic: Checked at run time >> a = [‘100’, 100.0] => [‘100’, 100.0] >> while i < 2 >> puts a[i].to_i >> i += 1 >> end 100 100 Technologically Impaired Duck
  • 22. >> def tell_the_truth >> true >> end # Last expression is return value Technologically Impaired Duck
  • 24. >> animals = [‘lions’, ‘tigers’] => [‘lions’, ‘tigers’] >> numbers = {1 => ‘one’, 2 => ‘two’} => {1=>”one”, 2=>”two”} # Symbols >> ‘string’.object_id => 3092010 >> ‘string’.object_id => 3089690 >> :string.object_id => 69618 >> :string.object_id => 69618
  • 25. >> def winning(options = {}) >> if(options[:profession] == :gambler) >> true >> else >> false >> end >> end => nil >> winning => false # {} optional for last parameter >> winning(:profession => :lawyer) => true
  • 26. YO DAWG I HEARD YOU LIKED CODE BLOCKS SO YOU COULD RUN CODE IN YOUR CODE
  • 27. >> 3.times { puts ‘hi’ } hi hi hi >> animals = [‘lions’, ‘tigers’] >> animals.each {|a| puts a} lions tigers # Blocks can be passed as parameters >> def pass_block(&block) >> end >> pass_block { puts ‘hi’ }
  • 28. Classes class MyClass def initialize(name) @name = name # instance var @@other = ‘’ # class var end def name return @name end # methods that check end in ? end my_class = MyClass.new(‘Name’) my_class.name # returns ‘Name’
  • 29. Modules module MyModule def name return @name end end class MyClass include MyModule def initialize(name) @name = name end end my_class = MyClass.new(‘Name’) my_class.name # returns ‘Name’
  • 30. Enumerable # Implements each method Comparable # Implements <=> (spaceship) method
  • 31. Open Classes # First invocation defines # Second invocation modifies class NilClass def blank? true end end class String def blank? self.size == 0 end end [‘’, ‘person’, nil].each {|a| puts a unless a.blank?} # outputs person