language of the month

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

Upload: elsu

Post on 23-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Language of the Month

Language of the MonthIf it’s December, it must be Ruby!

Adam Coffman and Brent Beer

Page 2: Language of the Month

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

Page 3: Language of the Month

Ruby – An Overview cont’dRuby 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.

Page 4: Language of the Month

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

Page 5: Language of the Month

A Comparison: C++

Page 6: Language of the Month

A Comparison: Ruby

What do you notice?

Page 7: Language of the Month

Variable Scope Using SigilsThe 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

Page 8: Language of the Month
Page 9: Language of the Month

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

Page 10: Language of the Month

It fails.

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

Page 11: Language of the Month

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!

Page 12: Language of the Month

Ruby OperatorsMany operators you will be familiar with from

C++ +, - , / , = , == , [ ], !

Many you may not be { }, =~, *, **, ..

Page 13: Language of the Month

Dynamic (Duck) TypingNo 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

Page 14: Language of the Month

Hashes / ArraysRuby 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.

Page 15: Language of the Month

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

Page 16: Language of the Month

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.

Page 17: Language of the Month

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

Page 18: Language of the Month

Virtual FunctionsRuby 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.

Page 19: Language of the Month

times_ndef self.something defines a Class methodThe following code defines 10 methods.

Page 20: Language of the Month

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.