If its December it must be Ruby Adam Coffman and Brent Beer Ruby An Overview Ruby is a multiparadigm programming language designed for ease of use and programmer happiness Ruby borrows concepts from scripting languages like ID: 393108
Download Presentation The PPT/PDF document "Language of the Month" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Language of the Month
If it’s December, it must be Ruby!Adam Coffman and Brent BeerSlide2
Ruby – An Overview
Ruby is a multi-paradigm programming language designed for ease of use and programmer happiness
Ruby borrows concepts from scripting languages like perl, object oriented languages like SmallTalk
, and functional languages like LispRuby was created by Yukihiro “matz” Matsumoto in 1995 in Japan
“Often people, especially computer engineers, focus on the machines. They think, "By doing this, the machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine will something something something." They are focusing on machines. But in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves.” -
MatzSlide3
Ruby – An Overview cont’d
Ruby is an interpreted language rather than a compiled one.Ruby has an interactive, real-time shell: IRB (interactive ruby)
Ruby features single inheritance only.Ruby favors blocks and closures over traditional loops.Slide4
Coming from C++
If you already know C++ you know many of the important concepts Ruby utilizesClasses, methods, loops, conditionals.
Most standard operatorsOO ideasYou probably don’t know
BlocksDuck typingDynamic
ProgrammingSlide5
A Comparison: C++Slide6
A Comparison: Ruby
What do you notice?Slide7
Variable Scope Using Sigils
The sigils $, @, @@ are used in ruby to denote variable scope.$ denotes a Global variable
@ denotes an instance variable@@ denotes a class variable<^> denotes a sombrero, often worn by Darth VaderSlide8Slide9
Everything is an object!
If we tried something like this in C++ or Java:Slide10
It fails.
This is because in C++ or Java, numbers and Strings are not Objects.
As such, they cannot posses methods or attributes. Slide11
Everything is an Object
In Ruby those would be perfectly valid operationsThis is because, like SmallTalk, Ruby is a purely Object Oriented language. Numbers, Strings, and Characters are all Objects.
When they say everything is an Object, they mean it! Slide12
Ruby Operators
Many operators you will be familiar with from C+++, - , / , = , == , [ ], !
Many you may not be{ }, =~, *, **, .. Slide13
Dynamic (Duck) Typing
No need to declare variable, argument, or return types.If it looks like a duck, walks like a duck, and quacks like a duck….it probably is a duck Slide14
Hashes / Arrays
Ruby has two basic data structures: Hashes and ArraysArrays are denoted by [ ] while Hashes are denoted by { }
Both use the Enumerable mixin, and thus have access to iterator
blocks such as inject, map, each, and reject.Hashes use key value pairs in much the same way traditional Arrays use indices. myHash[key] returns key=>value. Slide15
Blocks
Blocks are a concept Ruby borrows from functional programming languages.In Ruby, blocks are usually used in place of traditional loops.
Let’s look at two common types of blocks: each and map.Slide16
Just How Dynamic is Ruby?
A class is never finalized in Ruby, even system classes.It is never too late to open up a class and change it.
For instance, maybe we think that the Array class could use a sum method, adding it is trivial.Slide17
Metaprogramming
Metaprogramming is writing code that writes code.Ruby’s dynamic nature lends itself to
metaprogramming.We have actually already seen a built in example of Ruby
metaprogramming in the form of attr_accessor.Lets look at two more examples: virtual functions and “
n_times
” Slide18
Virtual Functions
Ruby has no built in functionality for implementing C++ style virtual functions (with a dynamic language, there are better solutions).However, using
metaprogramming, adding such functionality is trivial if you wanted to.Slide19
t
imes_ndef self.something
defines a Class methodThe following code defines 10 methods. Slide20
What
isnt’t Ruby?Ruby isn’t fast yet. Its current implementation is the slowest of the major scripting languages (
perl, python etc)The newest version of Ruby (1.9) is in progress and so far tests faster than
perl or python.There are several third party Ruby interpreters in development with even faster speeds. MagLev
is one of the most promising.