Introduction to Ruby & Modern
Programming
Christos Sotirelis
www.sotirelischristos.com
December 2015
What am I doing here?
Our goal
 Meet Ruby and modern programming
 See how to benefit from Ruby
 Learn some Ruby basics
 Prepare to dive in Rails and modern web applications
 Keep the journey going
What is Ruby?
What is Ruby?
o Dynamic, interpreted, object-oriented programming
language written in C
o Expressive and declarative
o Invented by Yukihiro “Matz” Matsumoto in 1996 but
was popularized by Ruby on Rails in 2005
o Influenced by Perl, Python, Smalltalk, Eiffel, Ada, Lisp
o Designed to make programmers happy
o Open source
Why another language?
Why Ruby?
o Very well documented
o Highly readable
o Multi-paradigm
o Memory-managed and garbage-collected
o Built-in data structures
o Many more..
Getting on Cloud9
 https://c9.io/
 “SIGN UP” or “TRY IT NOW”
 Create a new workspace
 “Ruby on Rails Tutorial” template
The IRB
On Cloud9’s terminal: irb
Alternatively, save your Ruby code with .rb
extension and run with: ruby <file.rb>
Ruby is multi-paradigm
Procedural
Procedural + Object-Oriented
OO Ruby
 In a website, a User is a class
 It has fields, such as an email and a password
 It also has methods, such as register() and login()
 We can create objects (or instances) of a class, such
as user1, user2 and so on
 We can also access their fields, like user1.email and
user1.password
 Most importantly, we can use class methods on our
objects, like user2.register() or user2.login()
 In a traditional, non-OO system, we would have to use
many different variables and methods for every user
we had (too much space)
OO Ruby
Procedural + Object-Oriented + Functional
Functional?
Chained methods
Java VS. Ruby
In Ruby, everything is an object!
is actually that:
So this..
Tell me 10 random methods of you
Hey 10, what are you?
More (expressive) Ruby..
Syntactic sugar
Parentheses and semicolons are optional
Dynamic typing
Duck typing
“If it walks like a duck and quacks like a duck, then
it must be a duck.”
Ruby conditionals
Ruby conditionals
Ruby loops
..much better this way!
Too old style..
Ranges :D
Homemade alphabet
Methods
Multiple returns plus
syntactic sugar
Arrays & hashes
Arrays & hashes
 Arrays and hashes in Ruby are more like collections
 They respond to same methods
 We can think of arrays as dynamic lists
 Arrays are also type-agnostic
Arrays
Hashes
 Hashes are pairs of unique keys and values
 Hashes (and arrays) include the Enumerable mixin
Advanced concepts
Open classes
 In Ruby, classes are never closed: methods can always
be added to an existing class
 Applies to all classes, including the standard, built-in
classes
 If not cautious, bad things are bound to happen
Metaprogramming & reflection
 In Ruby, code can be programmatically modified at
runtime
 Writing code that writes code
 Classes and methods know about themselves
Metaprogramming & reflection
Ruby gems
 Libraries in Ruby are called gems
 Can be installed with “gem install capistrano”
 View locally installed gems with “gem list”
Simple deck with cards
Save as cards.rb and run with “ruby cards.rb”
Look up on your own
 method_missing
 Mixins
 Introspection
 Exceptions
 Procs & lambdas
 Multithreading
 File I/O
 Sockets
 Regular expressions
 (Unit) Testing
More about Ruby
 https://www.ruby-lang.org
 https://www.codecademy.com
 http://www.tryruby.org
 http://www.ruby-doc.org
 http://www.sitepoint.com/ruby
Thank you!
you.ask(me) or you.email(me) if any_questions?
you.applause
www.sotirelischristos.com

Introduction to Ruby & Modern Programming