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

Viewers also liked

The Chillr Story - Evolution of a startup from Telecom to FinTech
The Chillr Story - Evolution of a startup from Telecom to FinTechThe Chillr Story - Evolution of a startup from Telecom to FinTech
The Chillr Story - Evolution of a startup from Telecom to FinTech
Sony Joy
 
Computer Hardware
Computer HardwareComputer Hardware
Computer Hardware
MobME Technical
 
The MobME Story - March 2010
The MobME Story - March 2010The MobME Story - March 2010
The MobME Story - March 2010Sony Joy
 
Introduction to JQuery
Introduction to JQueryIntroduction to JQuery
Introduction to JQuery
MobME Technical
 
Memcache
MemcacheMemcache
Memcache
MobME Technical
 
Ppt of prannav[startup village]
Ppt of prannav[startup village]Ppt of prannav[startup village]
Ppt of prannav[startup village]suthisha
 
How to set up and Configure Kannel, A quick start
How to set up and Configure Kannel, A quick startHow to set up and Configure Kannel, A quick start
How to set up and Configure Kannel, A quick startMobME Technical
 
GREEN KERALA
GREEN KERALAGREEN KERALA
GREEN KERALA
THANKACHAN V P
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load Balancer
MobME Technical
 
Startup village
Startup village Startup village
Startup village
Naveen Nave
 

Viewers also liked (10)

The Chillr Story - Evolution of a startup from Telecom to FinTech
The Chillr Story - Evolution of a startup from Telecom to FinTechThe Chillr Story - Evolution of a startup from Telecom to FinTech
The Chillr Story - Evolution of a startup from Telecom to FinTech
 
Computer Hardware
Computer HardwareComputer Hardware
Computer Hardware
 
The MobME Story - March 2010
The MobME Story - March 2010The MobME Story - March 2010
The MobME Story - March 2010
 
Introduction to JQuery
Introduction to JQueryIntroduction to JQuery
Introduction to JQuery
 
Memcache
MemcacheMemcache
Memcache
 
Ppt of prannav[startup village]
Ppt of prannav[startup village]Ppt of prannav[startup village]
Ppt of prannav[startup village]
 
How to set up and Configure Kannel, A quick start
How to set up and Configure Kannel, A quick startHow to set up and Configure Kannel, A quick start
How to set up and Configure Kannel, A quick start
 
GREEN KERALA
GREEN KERALAGREEN KERALA
GREEN KERALA
 
Web Server Load Balancer
Web Server Load BalancerWeb Server Load Balancer
Web Server Load Balancer
 
Startup village
Startup village Startup village
Startup village
 

Similar to Introduction to Ruby

Why everyone like ruby
Why everyone like rubyWhy everyone like ruby
Why everyone like ruby
Ivan Grishaev
 
What I Love About Ruby
What I Love About RubyWhat I Love About Ruby
What I Love About Ruby
Keith Bennett
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
Uģis Ozols
 
Rails console
Rails consoleRails console
Rails console
Reuven Lerner
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
Tushar Pal
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - Introduction
Kwangshin Oh
 
Minicurso Ruby e Rails
Minicurso Ruby e RailsMinicurso Ruby e Rails
Minicurso Ruby e Rails
SEA Tecnologia
 
دورتنا
دورتنادورتنا
دورتناmahersaif
 
اليوم السعيد
اليوم السعيداليوم السعيد
اليوم السعيدmahersaif
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
Lorna Mitchell
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Wen-Tien Chang
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Wen-Tien Chang
 

Similar to Introduction to Ruby (20)

Why everyone like ruby
Why everyone like rubyWhy everyone like ruby
Why everyone like ruby
 
What I Love About Ruby
What I Love About RubyWhat I Love About Ruby
What I Love About Ruby
 
Ruby 2.0
Ruby 2.0Ruby 2.0
Ruby 2.0
 
Rails console
Rails consoleRails console
Rails console
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Ruby basics
Ruby basicsRuby basics
Ruby basics
 
Ruby Programming Language - Introduction
Ruby Programming Language - IntroductionRuby Programming Language - Introduction
Ruby Programming Language - Introduction
 
Minicurso Ruby e Rails
Minicurso Ruby e RailsMinicurso Ruby e Rails
Minicurso Ruby e Rails
 
I Love Ruby
I Love RubyI Love Ruby
I Love Ruby
 
ruby
rubyruby
ruby
 
دورتنا
دورتنادورتنا
دورتنا
 
ruby
rubyruby
ruby
 
a course
a coursea course
a course
 
I Love Ruby
I Love RubyI Love Ruby
I Love Ruby
 
ruby
rubyruby
ruby
 
ruby
rubyruby
ruby
 
اليوم السعيد
اليوم السعيداليوم السعيد
اليوم السعيد
 
Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Ruby 入門 第一次就上手
Ruby 入門 第一次就上手Ruby 入門 第一次就上手
Ruby 入門 第一次就上手
 
Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介Ruby 程式語言綜覽簡介
Ruby 程式語言綜覽簡介
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 

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).