Presentation is loading. Please wait.

Presentation is loading. Please wait.

Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer.

Similar presentations


Presentation on theme: "Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer."— Presentation transcript:

1 Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer

2 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

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

4 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

5 A Comparison: C++

6 A Comparison: Ruby What do you notice?

7 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

8

9 Everything is an object! If we tried something like this in C++ or Java:

10 It fails. This is because in C++ or Java, numbers and Strings are not Objects. As such, they cannot posses methods or attributes.

11 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!

12 Ruby Operators  Many operators you will be familiar with from C++  +, -, /, =, ==, [ ], !  Many you may not be  { }, =~, *, **,..

13 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

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

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

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

17 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”

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

19 times_n  def self.something defines a Class method  The following code defines 10 methods.

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


Download ppt "Language of the Month If it’s December, it must be Ruby! Adam Coffman and Brent Beer."

Similar presentations


Ads by Google