Introduction to Ruby

Maciej Mensfeld
twitter: @maciejmensfeld
www: mensfeld.pl
e-mail: maciej@mensfeld.pl

A bit about me

  • 9 years of commercial exp in IT
  • 7 years with Ruby and Rails
  • I find Ruby quite useful
  • And I am running a blog mostly about Ruby related stuff

Please notify me if...

  • I speak 2 fast
  • I should repeat something
  • I should explain something better
  • You have any questions

What is Ruby?

Ruby is a powerful, flexible programming language you can use in web/Internet development, to process text, to create games, and as part of the popular Ruby on Rails web framework.

www.codecademy.com

Ruby is...

  • Open-source
  • High-level
  • Interpreted
  • Object-oriented
  • Easy to use
  • Great for prototyping (but not only)

Ruby is like an Iron-man

  • Shiny
  • Red
  • Quite handy
  • Heavy from time 2 time
  • Electric powered
  • Supported by many people

But where does Ruby come from?

  • Ruby comes from Japan
  • Created by Yukihiro Matsumoto (Matz) in 1993

Matz wants you to...

  • enjoy programming
  • be productive
  • be happy

That's why Ruby took good things among others from:

  • Python
  • Smalltalk
  • Perl
  • Lisp
  • Eiffel

How can we use Ruby?

  • Command line
  • Desktop applications
  • Web applications (mostly Rack based)
  • Mobile applications

Cracow Ruby community is really active

  • Thousands of developers
  • KRUG (Cracow Ruby Users Group)
  • Many workshops & courses (mostly free)
  • Many Cracow located companies use Ruby

Ruby loves to talk with other!

Who uses Ruby?

What Ruby is not?

  • Ruby is not the best solution to every problem though
  • Ruby is not suitable for really small and simple web apps
  • Ruby is not Python
  • Ruby is not the "better PHP"
  • Ruby is not something that works on Windows

Readability over convenience

Readability over convenience


public class HelloWorld{
  public static void main(String[] args){
    System.out.println("Hello World");
  }
}
          
vs

puts "Hello World"
          

Great syntax & objects, objects, objects...


3.times { puts "Ruby is cool" }
["Maciek", "John", "Anna"].first #=> "Maciek"
["Maciek", "John", "Anna"].last  #=> "Anna"
attr_accessor :name
          

"Anna".class #=> String
nil.class    #=> NilClass
1.class      #=> Integer
{}.class     #=> Hash
[].class     #=> Array
self.class   #=> Object
(0..9).class #=> Range
          

Classes, methods, objects


# Comments start with "#"
class Messenger
  def initialize(name)
     # Instance variables start with "@"
    @name = name
  end

  # Methods are public by default
  def hello
    puts "Hello #{@name}!"
  end
end
          
execution:

msg = Messenger.new("Maciek")
msg.hello #=> "Hello Maciek!"
          

Ruby loops


# Ruby each loop
friends.each{ |friend| puts friend }
          

// Same in C
for(i = 0; i < number_of_elements; i++)
{
  print element[i];
}
          

10.times { |i| puts i }
10.downto(1) { |i| puts i }
          

Ruby craziness - symbols

A Ruby symbol is a thing that has both a number (integer) representation and a string representation.

# Symbols are used in many places,
# for example you can use them to create hashes
user = { name: 'Maciej', email: 'maciej@mensfeld.pl' }
print user[:name] #=> "Maciej"
          
Symbols are way more efficient than strings because they act like singletons.

Ruby craziness - symbols


puts "name".object_id # => 8305060
puts "name".object_id # => 8295440
puts :name.object_id  # => 85148
puts :name.object_id  # => 85148
          

puts "name".object_id == "name".object_id # => false
puts :name.object_id == :name.object_id   # => true
          

DRY - Don't repeat yourself - Rubygems

If you need a certain functionality, someone probably built it already!

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them.

Ruby is not only about Rails

Ruby on Rails is great, but there's much more than that!

  • Huge variety of web frameworks like Sinatra, Padrino, ELDR, Lotus
  • RubyMotion - cross-platform mobile apps in Ruby
  • MobiRuby - Ruby platform for iOS development
  • Rhomobile - Motorola framework for mobile apps
  • EventMachine - fast, simple event-processing library for Ruby programs
  • And so on and so forth...

Sinatra in 30 seconds


gem install sinatra
          

# app.rb
require 'sinatra'

class HelloWorldApp < Sinatra::Base
  get '/' do
    'Hello, world!'
  end
end
          

# config.ru
require './app'
run HelloWorldApp
          

rackup

# Puma 2.11.3 starting...
# * Min threads: 0, max threads: 16
# * Environment: development
# * Listening on tcp://localhost:9292
          

THE END - Q & A

Maciej Mensfeld
twitter: @maciejmensfeld
www: mensfeld.pl
e-mail: maciej@mensfeld.pl