SlideShare a Scribd company logo
Introduction to Ruby
Ruby is...
●   A dynamic
●   open source
●   focus on simplicity and productivity
●   elegant syntax
Download & Install Now
    Windows
●   http://rubyforge.org/frs/download.php/47082/ruby186-2


    Linux
●   sudo apt-get install ruby irb rdoc
●   Interactive Ruby
●   Methods
●   Classes
●   Objects
●   Blocks and Looping
●   From PHP to Ruby
Interactive Ruby

irb(main):001:0>

irb(main):001:0> "Hello World"
=> "Hello World"



irb(main):002:0> puts "Hello World"
Hello World
=> nil
Module

irb(main):004:0> 3*2
=> 6

irb(main):005:0> 3**2
=> 9


irb(main):006:0> Math.sqrt(9)
=> 3.0


irb(main):007:0> a = 3 ** 2
=> 9
irb(main):008:0> b = 4 ** 2
=> 16
irb(main):009:0> Math.sqrt(a+b) => 5.0
Methods
irb(main):010:0> def h
irb(main):011:1> puts "Hello World!"
irb(main):012:1> end
=> nil

#Run the method

irb(main):013:0> h
Hello World!
=> nil
irb(main):014:0> h()
Hello World!
=> nil
Methods
irb(main):015:0> def h(name)
irb(main):016:1> puts "Hello #{name}!"
irb(main):017:1> end
=> nil
irb(main):018:0> h("Jins")
Hello Jins!
=> nil


irb(main):019:0> def h(name = "World")
irb(main):020:1> puts "Hello #{name.capitalize}!"
irb(main):021:1> end
=> nil
irb(main):022:0> h "jinson"
Hello Jinson!
=> nil
irb(main):023:0> h
Hello World!
=> nil
Classes


irb(main):024:0> class Greeter
irb(main):025:1> def initialize(name = "World")
irb(main):026:2> @name = name
irb(main):027:2> end
irb(main):028:1> def say_hi
irb(main):029:2> puts "Hi #{@name}!"
irb(main):030:2> end
irb(main):031:1> def say_bye
irb(main):032:2> puts "Bye #{@name}, come back soon."
irb(main):033:2> end
irb(main):034:1> end
=> nil
Create an Object

irb(main):035:0> g = Greeter.new("Jins")
=> #<Greeter:0x16cac @name="JIns">
irb(main):036:0> g.say_hi
Hi Jins!
=> nil
irb(main):037:0> g.say_bye
Bye Jins, come back soon.
=> nil
Cycling and Looping

@names.each do |name|
 puts "Hello #{name}!"
end



#C Code

for (i=0; i<number_of_elements; i++)
{
  do_something_with(element[i]);
}
Ruby & PHP - Similarities
●   Ruby is dynamically typed, like in PHP, so you don’t need to worry about having to
    declare variables.
●   There are classes, and you can control access to them like in PHP 5 (public, protected
    and private)
●   Some variables start with $, like in PHP (but not all)
●   There’s eval, too.
●   You can use string interpolating. Instead of doing ”$foo is a $bar”, you can do ”#{foo} is
    a #{bar}”—like in PHP, this doesn’t apply for single-quoted strings.
●   There’s heredocs
●   Ruby has exceptions, like PHP 5
●   There’s a fairly large standard library
●   Arrays and hashes work like expected, if you exchange array() for { and }: array('a' =>
    'b') becomes {'a' => 'b'}.
●   true and false behave like in PHP, but null is called nil
Ruby & PHP - Differences
●    There’s strong typing. You’ll need to call to_s, to_i etc. to convert between strings,
    integers and so on, instead of relying on the language to do it
●   Strings, numbers, arrays, hashes, etc. are objects. Instead of calling abs(-1) it’s -1.abs
●   Parentheses are optional in method calls, except to clarify which parameters go to
    which method calls
●   Variables are references.
●   There’s no abstract classes or interfaces
●   Hashes and arrays are not interchangeable
●   Only false and nil are false: 0, array() and "" are all true in conditionals.
●   Almost everything is a method call, even raise (throw in PHP).
Questions...?

More Related Content

PDF
Ruby on Rails 中級者を目指して - 大場寧子
PDF
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
PPSX
CodeIgniter L2 helper & libraries & form validation
PPTX
Code for Startup MVP (Ruby on Rails) Session 2
PDF
PHP 7 – What changed internally? (PHP Barcelona 2015)
PPSX
CodeIgniter L3 model & active record & template
PPTX
Coffee script
PDF
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails 中級者を目指して - 大場寧子
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
CodeIgniter L2 helper & libraries & form validation
Code for Startup MVP (Ruby on Rails) Session 2
PHP 7 – What changed internally? (PHP Barcelona 2015)
CodeIgniter L3 model & active record & template
Coffee script
Ruby on Rails Oracle adaptera izstrāde

Viewers also liked (10)

PDF
The Chillr Story - Evolution of a startup from Telecom to FinTech
PPT
Computer Hardware
PDF
The MobME Story - March 2010
PPT
Introduction to JQuery
PPT
Memcache
PPTX
Ppt of prannav[startup village]
PPT
How to set up and Configure Kannel, A quick start
PPTX
GREEN KERALA
PPT
Web Server Load Balancer
PPTX
Startup village
The Chillr Story - Evolution of a startup from Telecom to FinTech
Computer Hardware
The MobME Story - March 2010
Introduction to JQuery
Memcache
Ppt of prannav[startup village]
How to set up and Configure Kannel, A quick start
GREEN KERALA
Web Server Load Balancer
Startup village
Ad

Similar to Introduction to Ruby (20)

PPTX
Why everyone like ruby
ODP
What I Love About Ruby
PDF
Ruby 2.0
KEY
Rails console
PPTX
Ruby -the wheel Technology
PPTX
Ruby basics
PPTX
Ruby Programming Language - Introduction
KEY
Minicurso Ruby e Rails
PPTX
Introduction To PHP000000000000000000000000000000.pptx
PDF
ruby
PDF
I Love Ruby
PDF
a course
PDF
ruby
PDF
ruby
PDF
ruby
PDF
دورتنا
PDF
اليوم السعيد
PDF
I Love Ruby
PDF
Zend Certification Preparation Tutorial
PDF
Ruby 入門 第一次就上手
Why everyone like ruby
What I Love About Ruby
Ruby 2.0
Rails console
Ruby -the wheel Technology
Ruby basics
Ruby Programming Language - Introduction
Minicurso Ruby e Rails
Introduction To PHP000000000000000000000000000000.pptx
ruby
I Love Ruby
a course
ruby
ruby
ruby
دورتنا
اليوم السعيد
I Love Ruby
Zend Certification Preparation Tutorial
Ruby 入門 第一次就上手
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Cloud computing and distributed systems.
PDF
Electronic commerce courselecture one. Pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Approach and Philosophy of On baking technology
PDF
Empathic Computing: Creating Shared Understanding
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Programs and apps: productivity, graphics, security and other tools
The Rise and Fall of 3GPP – Time for a Sabbatical?
Building Integrated photovoltaic BIPV_UPV.pdf
Cloud computing and distributed systems.
Electronic commerce courselecture one. Pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
Chapter 3 Spatial Domain Image Processing.pdf
Network Security Unit 5.pdf for BCA BBA.
sap open course for s4hana steps from ECC to s4
Encapsulation_ Review paper, used for researhc scholars
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Approach and Philosophy of On baking technology
Empathic Computing: Creating Shared Understanding
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
“AI and Expert System Decision Support & Business Intelligence Systems”
Programs and apps: productivity, graphics, security and other tools

Introduction to Ruby

  • 2. Ruby is... ● A dynamic ● open source ● focus on simplicity and productivity ● elegant syntax
  • 3. Download & Install Now Windows ● http://rubyforge.org/frs/download.php/47082/ruby186-2 Linux ● sudo apt-get install ruby irb rdoc
  • 4. Interactive Ruby ● Methods ● Classes ● Objects ● Blocks and Looping ● From PHP to Ruby
  • 5. Interactive Ruby irb(main):001:0> irb(main):001:0> "Hello World" => "Hello World" irb(main):002:0> puts "Hello World" Hello World => nil
  • 6. Module irb(main):004:0> 3*2 => 6 irb(main):005:0> 3**2 => 9 irb(main):006:0> Math.sqrt(9) => 3.0 irb(main):007:0> a = 3 ** 2 => 9 irb(main):008:0> b = 4 ** 2 => 16 irb(main):009:0> Math.sqrt(a+b) => 5.0
  • 7. Methods irb(main):010:0> def h irb(main):011:1> puts "Hello World!" irb(main):012:1> end => nil #Run the method irb(main):013:0> h Hello World! => nil irb(main):014:0> h() Hello World! => nil
  • 8. Methods irb(main):015:0> def h(name) irb(main):016:1> puts "Hello #{name}!" irb(main):017:1> end => nil irb(main):018:0> h("Jins") Hello Jins! => nil irb(main):019:0> def h(name = "World") irb(main):020:1> puts "Hello #{name.capitalize}!" irb(main):021:1> end => nil irb(main):022:0> h "jinson" Hello Jinson! => nil irb(main):023:0> h Hello World! => nil
  • 9. Classes irb(main):024:0> class Greeter irb(main):025:1> def initialize(name = "World") irb(main):026:2> @name = name irb(main):027:2> end irb(main):028:1> def say_hi irb(main):029:2> puts "Hi #{@name}!" irb(main):030:2> end irb(main):031:1> def say_bye irb(main):032:2> puts "Bye #{@name}, come back soon." irb(main):033:2> end irb(main):034:1> end => nil
  • 10. Create an Object irb(main):035:0> g = Greeter.new("Jins") => #<Greeter:0x16cac @name="JIns"> irb(main):036:0> g.say_hi Hi Jins! => nil irb(main):037:0> g.say_bye Bye Jins, come back soon. => nil
  • 11. Cycling and Looping @names.each do |name| puts "Hello #{name}!" end #C Code for (i=0; i<number_of_elements; i++) { do_something_with(element[i]); }
  • 12. Ruby & PHP - Similarities ● Ruby is dynamically typed, like in PHP, so you don’t need to worry about having to declare variables. ● There are classes, and you can control access to them like in PHP 5 (public, protected and private) ● Some variables start with $, like in PHP (but not all) ● There’s eval, too. ● You can use string interpolating. Instead of doing ”$foo is a $bar”, you can do ”#{foo} is a #{bar}”—like in PHP, this doesn’t apply for single-quoted strings. ● There’s heredocs ● Ruby has exceptions, like PHP 5 ● There’s a fairly large standard library ● Arrays and hashes work like expected, if you exchange array() for { and }: array('a' => 'b') becomes {'a' => 'b'}. ● true and false behave like in PHP, but null is called nil
  • 13. Ruby & PHP - Differences ● There’s strong typing. You’ll need to call to_s, to_i etc. to convert between strings, integers and so on, instead of relying on the language to do it ● Strings, numbers, arrays, hashes, etc. are objects. Instead of calling abs(-1) it’s -1.abs ● Parentheses are optional in method calls, except to clarify which parameters go to which method calls ● Variables are references. ● There’s no abstract classes or interfaces ● Hashes and arrays are not interchangeable ● Only false and nil are false: 0, array() and "" are all true in conditionals. ● Almost everything is a method call, even raise (throw in PHP).