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

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
 

Recently uploaded (20)

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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].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
 

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