Features of the Ruby Language

Hello World

www.flickr.com/photos/rappensuncle/85831251/

Hello World


def hello_world puts 'Hello World' end hello_world
Hello World

Hello World


def hello_world # Returns a String 'Hello World' # Inline comment end puts hello_world
Hello World

Strings

www.flickr.com/photos/chris_radcliff/93701651/

Strings


a = 'Hello World' b = "The string #{a} \n is #{a.length()} characters long." puts a print b
Hello World The string Hello World is 11 characters long.

Numbers

www.flickr.com/photos/wrhowell/41684973/

Numbers


n = 1 n = 2.5 + n puts n
3.5

Numbers


n = 1 m = 10000 * 200000 puts n.class puts m.class puts -12.abs
Fixnum Bignum 12

Objects

www.flickr.com/photos/kubina/50366633/

Objects


puts -12.abs

System.out.println(Math.abs(-12));

Objects


Object ==, ===, =~, __id__, __send__, class, clone, dclone, display, dup, eql?, equal?, extend, freeze, frozen?, hash, id, initialize_copy, inspect, instance_eval, instance_of?, instance_variable_get, instance_variable_set, instance_variables, is_a?, kind_of?, method, methods, nil?, object_id, private_methods, protected_methods, public_methods, remove_instance_variable, respond_to?, send, singleton_method_added, singleton_method_removed, singleton_method_undefined, singleton_methods, taint, tainted?, to_a, to_s, type, untaint

The word "object" is commonly used to refer to an instance of a class.

Objects


a = nil a.inspect => "nil" a.class => NilClass a.nil? => true a = 2 => 2 a.nil? => false

Variables

www.flickr.com/photos/kubina/64654463/

Variables


greeting1 = 'Good Morning' greeting2 = greeting1 greeting1[5..12] = 'Day' puts greeting1 puts greeting2
Good Day Good Day

Constants


PI = 3.14 PI = 3.14159 => warning: already initialized constant PI

Duck Typing

www.flickr.com/photos/njh/13349747/

Dynamic Typing


num = 1 str = '2' num + str => Type Error: String can't be coerced into Fixnum num + str.to_i => 3

Duck Typing


def display_length(object) puts object.length() end display_length('Hello') => 5 display_length(['H', 'e', 'l', 'l', 'o']) => 5 display_length(23) => NoMethodError: undefined method `length' for 23:Fixnum

Naming Conventions


Methods


def double(num) num * 2 end puts(double(4)) puts double 5
8 10

Control Structures

www.flickr.com/photos/kubina/114126906/

Control Structures


if (foo) puts 'foo' elsif (bar) puts 'bar' else puts 'rar' end name = case when num == 1: "one" when num == 2: "two" when num == 3: "three" else num.to_s end

Control Structures


if num > 0 puts 'positive' end puts 'positive' if num > 0 num = num + num while num < 100

Class

www.flickr.com/photos/kubina/110123784/

Class


class Ball def initialize(color, radius, material) @color = color @radius = radius @material = material end end ball = Ball.new("red", 5, "plastic")

Class


class Ball def to_s "#{@color.capitalize} ball made of #{@material}." end end puts ball.to_s
Red ball made of plastic.

Class


class Ball attr_reader :color, :radius, :material def color=(color) @color = color end end ball.color => "red" ball.color=("blue") => "blue"

Class


class BasketBall < Ball def initialize(color, radius, material, brand) super(color, radius, material) @brand = brand end end

Mixins


module Formulas PI = 3.14159 def surface_area(radius) (radius**2) * PI end end

On to Collections and Blocks

www.flickr.com/photos/nabeel/50480403/