SlideShare a Scribd company logo
1 of 32
Download to read offline
Seven Languages in Seven Days: Ruby
            prepared by @zachleat
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

Similar to Seven Languages in Seven Days: Ruby

What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)What I learned from Seven Languages in Seven Weeks (IPRUG)
What I learned from Seven Languages in Seven Weeks (IPRUG)
Kerry Buckley
 
Desarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutosDesarrollando aplicaciones web en minutos
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
Ruby 程式語言入門導覽
Ruby 程式語言入門導覽Ruby 程式語言入門導覽
Ruby 程式語言入門導覽
Wen-Tien Chang
 
python chapter 1
python chapter 1python chapter 1
python chapter 1
Raghu nath
 
Python chapter 2
Python chapter 2Python chapter 2
Python chapter 2
Raghu nath
 

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

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

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 

Seven Languages in Seven Days: Ruby

  • 1. Seven Languages in Seven Days: Ruby prepared by @zachleat
  • 2.
  • 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