Slide 1

Slide 1 text

RUBY & RAILS

Slide 2

Slide 2 text

GET TO KNOW RUBY • 1993 • Japan • Matz (Matsumoto Yukihiro) • Perl, Smalltalk, Eiffel, Ada, Lisp • Scripting language • Goals: truly OO, simple syntax, GC, closures, portable • Multi paradigm (functional, OO, imperative) • Dynamically strongly typed • ~2000: Ruby 1.8 -> Duck Typing, Ruby on Rails, The Pickaxe Book • A favorite of Martin Fowler ;)

Slide 3

Slide 3 text

“ I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language. -Matz @ Google Tech Talk (2008)

Slide 4

Slide 4 text

PHILOSOPHY • Designed to minimize work, confusion and to be enjoyable • Principles of good user interface design • Emphasize human needs over computer needs • MISWAN - Community

Slide 5

Slide 5 text

“ People are part of the system. The design should match the user's experience, expectations, and mental models. -Saltzer and Kaashoek “Principles of computer system design: an introduction“ (2009)

Slide 6

Slide 6 text

POLA • Principle of least astonishment / surprise • Associated with Ruby, but!

Slide 7

Slide 7 text

“ Everyone has an individual background. […] The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. -Matz in "The Philosophy of Ruby” by Venners (2015)

Slide 8

Slide 8 text

RUBY IMPLEMENTATIONS • Reference implementation: MRI - Matz’s Ruby Interpreter a.k.a. CRuby alternatives
 • complete list https://github.com/cogitator/ruby- implementations/wiki/List-of-Ruby-implementations • most notable list https://www.ruby-lang.org/en/about/ in “Other Implementations of Ruby” • JRuby on top of JVM • Rubinius - Ruby in Ruby • mruby - embedded, led by Matz

Slide 9

Slide 9 text

IRB REPL • Interactive Ruby Shell • Read - Eval - Print Loop • Command line tool with command history, line editing, network access

Slide 10

Slide 10 text

BASIC SYNTAX - NAMING CONVENTIONS • Use English • snake_case for symbols, methods, variables • CamelCase for classes and modules (keep acronyms) • snake_case for file and directory names • Name of the file = name of the class -> One class, one file • SCREAMING_SNAKE_CASE for constants • Predicate methods end with a question mark, no prefix (ex. user.coach_subscription?) • Potentially dangerous methods end with an exclamation mark (a bang) • # comment • https://github.com/bbatsov/ruby-style-guide

Slide 11

Slide 11 text

BASIC SYNTAX - DECLARATIONS • variable (local, instance, class, global)
 
 greeting = “hi”
 • method
 
 def say_hi
 puts “hi”
 end

Slide 12

Slide 12 text

BASIC SYNTAX - CONTROL STRUCTURES if condition1 something elsif condition2 something else else finally end

Slide 13

Slide 13 text

BASIC SYNTAX - CONTROL STRUCTURES if condition something end

Slide 14

Slide 14 text

BASIC SYNTAX - CONTROL STRUCTURES something if condition

Slide 15

Slide 15 text

BASIC SYNTAX - CONTROL STRUCTURES unless condition something else something else end if !condition something else something else end

Slide 16

Slide 16 text

BASIC SYNTAX - CONTROL STRUCTURES a = 2 case when a == 1, a == 2 puts "a is one or two" when a == 3 puts "a is three" else puts "I don't know what a is" end

Slide 17

Slide 17 text

BASIC SYNTAX - LOOPS for i in 1..8 do puts i end 1.upto(5) do |i| puts i end 5.times do |i| puts i end

Slide 18

Slide 18 text

BASIC SYNTAX - LOOPS array.each do |elem| puts elem end for elem in [1, 2, 3] do puts elem end

Slide 19

Slide 19 text

BASIC SYNTAX - LOOPS i = 0 until i == 5 do puts i i += 1 end i = 0 while i < 5 do puts i i += 1 end

Slide 20

Slide 20 text

BASIC SYNTAX - LOOPS puts i while i < 5 puts i until i == 5

Slide 21

Slide 21 text

DATA TYPES IN RUBY-CORE • Numeric: Integer, Float, Complex, Rational • Collections: Array, Hash, Range, Struct, Enumerator • Text: String, Symbol • Boolean: TrueClass, FalseClass, NilClass

Slide 22

Slide 22 text

DATA TYPES - STRINGS "I am a string" 'I am also a string'

Slide 23

Slide 23 text

DATA TYPES - SYMBOLS :symbol :"yup still a symbol" "me too".to_sym Like a string, but instantiated only once per application execution

Slide 24

Slide 24 text

DATA TYPES - SYMBOLS "test".object_id => 70308044363980 "test".object_id => 70308044357840 :test.object_id => 196968 :test.object_id => 196968

Slide 25

Slide 25 text

DATA TYPES - ARRAYS Initialization [1, "two", 3] Array.new() # empty Array.new(quantity, content) %w[array of only words] Usage foo = [1, "two", 3] foo[1] # => "two"

Slide 26

Slide 26 text

DATA TYPES - HASHES Initialization {one: "value", two: 20, three: [1, 2, 3]} {"one" => "value", "three" => [1, 2]} Hash.new() # empty Hash.new(default) # what to return if no key Usage foo = {first: "one", two: 2} foo[:first] # => "one" foo["first"] # => nil

Slide 27

Slide 27 text

HANDS ON #1 • Anna is 38, she is from Germany, she prefers cats, earns 40k per year, she studied Economics • Mike is 27, he is from China, he prefers dogs, earns 40k per year, he studied Medicine • Svetla is 35, she is from Bulgaria, she prefers cats, earns 80k per year, she studied Electrical Engineering • Theo is 60, he is from Germany, he doesn’t like pets, earns 50k per year, he studied Law 1. Create appropriate data structures 2. Find all people who are from Europe 3. Find all people who earn between 45k and 70k per year 4. Find all people who don’t like pets

Slide 28

Slide 28 text

CREDITS Monica Giambitto - 2017 This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. I don't own the right to the images unless otherwise stated. If you feel you rights have been violated, please contact me at [email protected] To view a copy of this license visit 
 http://creativecommons.org/licenses/by-nc-sa/4.0/ You can find the slides on speakerdeck.com/nirnaeth

Slide 29

Slide 29 text

THANKS! @KFMOLLI GITHUB.COM/NIRNAETH SPEAKERDECK.COM/NIRNAETH