Data types 

(also named Ruby objects)


String    =>  "Hello world!"


Integer    =>  2


Float    =>  2.75


Range    =>  ( 1..10 )


Array    =>  [ "John", "Ringo", "Paul", "George" ] or [ 1, 3, 4, 10 ]


Hash    =>  { "Name" => "Amy", "Age" => 27 }


=> Each objects has its own methods

Fixnum (integer)

basic operators  =>  +   -   /   %

10 % 2    =>  0


.even? and .odd?    =>  true or false


10.to_f    =>  10.0


Float

3.14159.round(2)    =>  3.14


2.85.to_i    =>  2


String


"Hello world".include? "l"    =>  true


"     Hello   ".strip    =>   "Hello"


"Hello".chars    =>  [ "H", "e", "l", "l", "o" ]


"What the hell is he doing ?".gsub(/hell/, "****")    =>  "What's the **** is he doing ?"

Array


[ "John", "Paul", "Ringo", "George" ].sample(2)   => [ "John", "Ringo" ]


[ 4, 6, 1 ].sort    => [ 1, 4, 6 ]


[ 1, 2, 3 ].reverse    => [ 3, 2, 1 ]


[ "Paul, "John", "Ringo", "George" ].size    => 4



What is a method ?


Factoring your code 

It is a way to call your code 

Examples of methods

def fullname( firstname, lastname )

    name = firstname.capitalize + " " + lastname.capitalize

    return name

end


def count_words( sentence )

    length = sentence.split(" ").size

    return length

end


puts fullname( "donald", "TRUMP" )

puts fullname( "HILLARY", "clinton" )


puts count_words( "I have a dream that one day this nation will rise up and live out the true meaning of its creed")

puts count_words( "Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime")

Controlling the flow

if/else statement

if condition

    # code that will be executed

end


if !condition

    # code tht will be executed if condition is false

end


This is the same as below:


unless condition

    # code that will be executed if condition is false

end

if condition

    # code executed if condition is true

else

    # code executed if condition is false

end



if condition

    # ...

elsif another condition

    # ...

else another condition

    # ...

end



do_something if condition

do_something unless condition

Multiple conditions

AND => &&

OR ==> ||

&& means AND


true && false    => false

true && true    => true

false && true    => false

false && false    => false

true && true && false    => false

|| means OR


true || false    => true

true || true    => true

false || true    => true

false || false    => false

true || true || false    => true

|| means OR


true || false    => true

true || true    => true

false || true    => true

false || false    => false

true || true || false    => true

Create a presentation like this one
Share it on social medias
Share it on your own
Share it on social medias
Share it on your own

How to export your presentation

Please use Google Chrome to obtain the best export results.


How to export your presentation

Basic programming

by greniergodard

15 views

Public - 11/6/16, 8:42 PM