200 likes | 407 Views
Language of the Month. If it’s December, it must be Ruby! Adam Coffman and Brent Beer. Ruby – An Overview. Ruby is a multi-paradigm programming language designed for ease of use and programmer happiness
E N D
Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer
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 Lisp Ruby 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.” -Matz
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.
Coming from C++ • If you already know C++ you know many of the important concepts Ruby utilizes • Classes, methods, loops, conditionals. • Most standard operators • OO ideas • You probably don’t know • Blocks • Duck typing • Dynamic Programming
A Comparison: Ruby What do you notice?
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 Vader
Everything is an object! If we tried something like this in C++ or Java:
It fails. This is because in C++ or Java, numbers and Strings are not Objects. As such, they cannot posses methods or attributes.
Everything is an Object In Ruby those would be perfectly valid operations This 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!
Ruby Operators • Many operators you will be familiar with from C++ • +, - , / , = , == , [ ], ! • Many you may not be • { }, =~, *, **, ..
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
Hashes / Arrays Ruby has two basic data structures: Hashes and Arrays Arrays 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.
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.
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.
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”
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.
times_n def self.something defines a Class method The following code defines 10 methods.
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.