History of Ruby

Download Report

Transcript History of Ruby

Why Ruby ?
Hesham Kanany
[email protected]
Scope
• History of Ruby.
• Where can you use Ruby?
• General Features.
• Ruby: the Language.
• Advantages using Ruby against other languages.
• Some Coding Conventions.
• Now what?
History of Ruby
●
●
Created (in Japan) by Yukihiro Matsumoto, popularly
called Matz.
Matz has stated, "I wanted a scripting language that
was more powerful than Perl, and more object-oriented
than Python. That's why I decided to design my own
language"
●
Released to the public in 1995.
●
Licensed under GPL or Ruby terms.
About Ruby
•Ruby is… A dynamic, open source programming language with a
focus on simplicity and productivity. It has an elegant syntax that is
natural to read and easy to write.
At a Google Tech Talk in 2008 Matz further stated, "I hope to see
Ruby help every programmer in the world to be productive, and to
enjoy programming, and to be happy. That is the primary purpose
of Ruby language.
●
•Matz has often said that he is “trying to make Ruby natural, not
simple,” in a way that mirrors life.
Building on this, he adds:
Ruby is simple in appearance, but is very complex inside, just like
our human body.
Write more understandable code in less lines.
●
Where can you use Ruby?
●
Text processing.
●
web apps.
●
general system administration.
●
AI and math research.
General Features
●
High level language.
●
True OO (everything’s an object!).
●
Interpreted.
●
Highly portable, works on Linux, UNIX, DOS, Windows
95/98/NT/2K, Mac, etc.
Ruby: the Language
●
No multiple inheritance, but modules allow the importing of
methods.
●
Has garbage collection.
●
Exception handling, like Java.
●
Any class or instance can be extended any time (even during
runtime) .
●
Allows operator overloading.
●
Can be extended with Ruby or low-level C.
●
No type declaration.
Advantages using Ruby against
other languages
●
Allow pass by code block.
●
Support Regular Expression.
●
Cleaner code.
●
No need to declare variables.
●
Simple syntax (semi-colon is optional).
●
Every thing is an object, even a number.
●
Simple object get/set methods declaration.
Some Coding Conventions
Just in Ruby :)
●
puts x if !x>5
●
puts x unless x>5
●
●
●
10.times do
------End
5.upto 12 do
------end
100.downto 32 do
------end
Just in Ruby :) (continue)
cat=Animal.new('myCat')
def cat.sleep(mouse) #Define a function on the object level
------------End
class Animal
def name =(new_name)
@name =new_name
end
end # acts as a setter
dog=Animal.new('wolf')
dog.name() → 'wolf'
dog.name= 'lolo'
dog.name → 'lolo'
Ranges
1..10
Inclusive range
my_range = 1 .. 10
# Creates a range from 1 to 10
Non-inclusive range
my_range = 1 … 10
1...10 # Creates a range from 1 to 9
('cab'..'car').to_a
=> ["cab", "cac", "cad", "cae", "caf", "cag", "cah", "cai",
"caj", "cak", "cal", "cam",
"can", "cao", "cap", "caq", "car"]
Data Type Conversion
Converting to an Array:
var_data_type.to_a
Converting to an String:
var_data_type.to_s
More (guess!):
var_data_type.to_i
var_data_type.to_f
Everything's an Object
Methods can be applied to data directly – not just on variables
holding data
Example:
5.to_s will return "5"
Iterators
array_or_range = value
array_or_range.each { |x|
print x
}
Example:
my_range = 1..5
my_range.each { |x|
print x
}
Functions
Functions begin with the keyword def
def function_name([args])
code block
end
Example:
def print_name(name='Ruby')
print name
end
OO Ruby
Classes are containers for static data members and
functions
Declared using the class keyword. All class names
should begin with a capital letter
Constructor declared using the initialize keyword
Class variables precede with an “@”
Objects created using the new method
Now what?
What you can do now?
Write simple Ruby programs
What you have to do?
Find a problem, and Ruby it!
Thank you!
Questions?