$30 off During Our Annual Pro Sale. View Details »

Ruby.new

jakefolio
February 28, 2013

 Ruby.new

Time to get off the Rails and learn the core foundation of the Rails Framework, Ruby. Ruby is a clean, flexible and easy to read language. During this session you will learn
basic syntax, OOP and the tools/libs the Ruby Way.

jakefolio

February 28, 2013
Tweet

More Decks by jakefolio

Other Decks in Programming

Transcript

  1. Ruby.new
    INTRODUCTION TO RUBY
    Thursday, February 28, 13

    View Slide

  2. Thursday, February 28, 13

    View Slide

  3. Thursday, February 28, 13

    View Slide

  4. Thursday, February 28, 13

    View Slide

  5. Thursday, February 28, 13

    View Slide

  6. Thursday, February 28, 13

    View Slide

  7. jakefolio
    http://jakefolio.com
    [email protected]
    Jake Smith
    Thursday, February 28, 13

    View Slide

  8. http://bit.ly/YKoi0z
    Links and Resources
    Thursday, February 28, 13

    View Slide

  9. History
    1
    Thursday, February 28, 13

    View Slide

  10. http://www.flickr.com/photos/rrrodrigo/2393323487/
    Yukihiro Matsumoto
    SmallTalk
    Perl
    LISP
    Python
    Created Ruby in 1995 in Japan.
    The first english speaking user
    group was not created until Ruby
    1.3 (1999)
    Thursday, February 28, 13

    View Slide

  11. Ruby
    2
    Thursday, February 28, 13

    View Slide

  12. Everything is
    an Object
    Thursday, February 28, 13

    View Slide

  13. Everything....
    "Hello World".class
    # => String
    5.class
    # => Fixnum
    5000000000000000000.class
    # => Bignum
    3.14.class
    # => Float
    nil.class
    # => NilClass
    true.class
    # => TrueClass
    false.class
    # => FalseClass WAT?
    Thursday, February 28, 13

    View Slide

  14. Variables
    foo_bar
    # => Local Variable
    Foobar
    # => Constant
    @foo_bar
    # => Instance Variable
    @@foo_bar
    # => Class Variable
    $foo_bar
    # => Global Variable
    Thursday, February 28, 13

    View Slide

  15. Conventions
    Thursday, February 28, 13

    View Slide

  16. current_user = User.find(1)
    random_ids = [2,6,4,6,36,67,77,5,4]
    random_ids.uniq
    random_ids
    # => [2, 6, 4, 6, 36, 67, 77, 5, 4]
    random_ids.uniq!
    random_ids
    # => [2, 6, 4, 36, 67, 77, 5]
    random_ids.include? 4
    # => true
    Lower case and _ seperated
    Modify in place
    Expect a boolean response
    Thursday, February 28, 13

    View Slide

  17. Data Types
    3
    Thursday, February 28, 13

    View Slide

  18. String
    "Grumpy wizards make toxic brew"
    # => Grumpy wizards make toxic brew
    subject + " make toxic brew"
    # => Grumpy wizards make toxic brew
    "#{subject} make toxic brew"
    # => Grumpy wizards make toxic brew
    '#{subject} make toxic brew'
    # => #{subject} make toxic brew
    content = <One morning, when Gregor Samsa woke from troubled dreams,
    he found himself transformed in his bed into a horrible vermin.
    He lay on his armour-like back, and if he lifted his head a little
    END
    Thursday, February 28, 13

    View Slide

  19. String
    "Grumpy wizards make toxic brew".length
    # => 30
    "Grumpy wizards make toxic brew".reverse
    # => "werb cixot ekam sdraziw ypmurG"
    "Grumpy wizards make toxic brew".upcase
    # => "GRUMPY WIZARDS MAKE TOXIC BREW"
    "".methods
    # => Shows all methods available to String, too many to list ;)
    Thursday, February 28, 13

    View Slide

  20. Number
    0xfff
    # => 4095
    1.0e-05
    "%f" % 1.0e-5
    # => "0.000010"
    5 ** 5
    # => 3125
    128 / 10
    # => 12
    Where’s my remainder?
    String Formatting
    Thursday, February 28, 13

    View Slide

  21. Number
    (128 / 10).to_f
    # => 12.0
    128.0 / 10
    # => 12.8
    128.to_f / 10
    # => 12.8
    Thursday, February 28, 13

    View Slide

  22. Range
    (1..100).each { |v| puts v }
    # => 1, 2, 3, 4...97, 98, 99, 100
    #
    (1...100).each { |v| puts v }
    # => 2, 3, 4, 5...96, 97, 98, 99
    (1...100).to_a.include? 100
    # => false
    ('a'..'z').each { |v| puts v }
    # => a, b, c....x, y, z
    ('a'..'z').covers?('e')
    # => true
    Thursday, February 28, 13

    View Slide

  23. Regex
    username = "jsmith92@"
    match_username = Regexp.new('/^[a-zA-Z0-9_]+$/')
    match_username = /^[a-zA-Z0-9_]+$/
    username.match match_username
    # => nil
    username = "jsmith92"
    username.match match_username
    # => #
    find_date = "The conference is taking place on 2013-02-01"
    find_date =~ /\d{4}-\d{1,2}-\d{1,2}/
    # => 34
    Thursday, February 28, 13

    View Slide

  24. Regex
    content = "Welcome to an intro of PHP. PHP the greatest language
    EVAR!"
    content.sub('PHP', 'Ruby')
    # => "Welcome to an intro of Ruby. PHP the greatest language EVAR!"
    content.gsub('PHP', 'Ruby')
    # => "Welcome to an intro of Ruby. Ruby the greatest language EVAR!"
    content.gsub(/php/i, 'Ruby')
    # => "Welcome to an intro of Ruby. Ruby the greatest language EVAR!"
    Thursday, February 28, 13

    View Slide

  25. Symbols
    user = { :username => 'jdoe' }
    user[:username]
    # => "jdoe"
    "get".to_sym
    # => :get
    :username.object_id
    # => 458088
    user2 = { :username => 'jsmith' }
    :username.object_id
    # => 458088
    It’s a String!
    Not Really
    Thursday, February 28, 13

    View Slide

  26. Array
    greeting = Array.new
    greeting = []
    greeting.push 'Hello'
    # => ["Hello"]
    greeting << 'Confoo'
    # => ["Hello", "Confoo"]
    greeting.join(' ')
    # => "Hello Confoo"
    %w(Hello Confoo Attendees)
    # => ["Hello", "Confoo", "Attendees"]
    ["Hello", "Confoo", [1, 2, 3]].flatten
    # => ["Hello", "Confoo", 1, 2, 3]
    Perl inspired
    Alternate Notation
    Thursday, February 28, 13

    View Slide

  27. Hash
    user = { :name => 'Jake', :location => [32.7828, 96.8039] }
    puts user[:name]
    # => Jake
    puts user[:random]
    # => nil
    user = Hash.new 'Oh noes I\'m empty'
    puts user
    # => {}
    user[:username]
    # => "Oh noes I'm empty"
    user.empty?
    # => true
    Thursday, February 28, 13

    View Slide

  28. Hash
    New in Ruby 1.9
    current_user = {
    :first_name => 'Jake',
    :last_name => 'Smith',
    :username => 'jsmith'
    }
    current_user = {
    first_name: 'Jake',
    last_name: 'Smith',
    username: 'jsmith'
    }
    current_user.first
    # => [:first_name, "Jake"]
    New JSON Inspired Format
    Order Maintained
    Thursday, February 28, 13

    View Slide

  29. Conditionals
    and Loops
    4
    Thursday, February 28, 13

    View Slide

  30. Conditionals
    if @post.published? and @post.approved?
    # Show the post
    else
    # DO NOT show post
    end
    unless @user.access?
    # Access Denied
    end
    if [email protected]?
    # Access Denied
    end
    if format == 'xml'
    # Base output on XML
    elsif format == 'json'
    # Do some JSON Hotness
    end
    @reservation.save if @location.available?
    redirect_to users_path and return unless @user.save
    Same Conditional
    Thursday, February 28, 13

    View Slide

  31. Conditionals
    if ""
    puts "Empty String Works"
    end
    if 0
    puts "Zero Works"
    end
    if nil
    puts "Nil Works"
    end
    if false
    puts "False Works"
    end
    Thursday, February 28, 13

    View Slide

  32. Conditionals
    case ranking
    when 1..3
    puts "You're at an elite level"
    when 4..10
    puts "You made it to the second round!"
    else
    puts "Better luck next year!"
    end
    Thursday, February 28, 13

    View Slide

  33. Iterations
    users = [
    { name: 'John', username: 'jdoe' },
    { name: 'Jane', username: 'jadoe' },
    { name: 'Jake', username: 'jsmith' }
    ]
    users.each do |user|
    puts user[:name]
    end
    users.each_with_index do |user, index|
    puts "[#{index}] #{user[:name]}"
    end
    user = "Random User Variable"
    for user in users
    puts user[:name]
    end
    puts user
    # => {:name=>"Jake", :username=>"jsmith"}
    User is still defined?
    Thursday, February 28, 13

    View Slide

  34. Looping
    3.times { puts "Hello World" }
    # => Hello World
    # => Hello World
    # => Hello World
    bottles = 99
    while bottles > 0
    # take one down pass it...
    puts bottles.to_s + " bottles of beer on the wall"
    bottles -= 1
    end
    until file.eof?
    # Work with line content
    end
    Thursday, February 28, 13

    View Slide

  35. Blocks, Procs,
    and Lambdas
    5
    Thursday, February 28, 13

    View Slide

  36. Blocks in the Wild
    squared = [1,2,3,4].map do |element|
    element ** 2
    end
    puts squared.join(', ')
    # => 1, 4, 9, 16
    Thursday, February 28, 13

    View Slide

  37. class Array
    def square!(&block)
    self.each_with_index do |value, index|
    self[index] = yield(value)
    end
    self
    end
    end
    my_numbers = [1,2,3,4]
    my_numbers.square! do |element|
    element ** 2
    end
    puts my_numbers.join(', ')
    # => 1, 4, 9, 16
    Thursday, February 28, 13

    View Slide

  38. Block
    Proc
    Lambda
    Thursday, February 28, 13

    View Slide

  39. Procs and Lambdas
    def test_proc
    Proc.new { return "Hey look I'm a Proc" }.call
    "Finished Proc Test"
    end
    def test_lambda
    lambda { return "Hey look I'm a Lambda" }.call
    "Finished Lambda Test"
    end
    puts test_proc
    # => Hey look I'm a Proc
    puts test_lambda
    # => Finished Lambda Test
    Y U NO FINISH?
    Thursday, February 28, 13

    View Slide

  40. Classes
    6
    Thursday, February 28, 13

    View Slide

  41. Creating a class
    class Asset
    def initialize(path)
    @path = path
    @dimensions = [32, 20]
    end
    end
    image = Asset.new('/images/hello.jpg')
    Thursday, February 28, 13

    View Slide

  42. Accessors
    class Asset
    attr_accessor :dimensions
    attr_reader :path
    def initialize(path)
    @path = path
    @dimensions = [32, 20]
    end
    end
    image = Asset.new('/images/hello.jpg')
    puts image.dimensions.join('x')
    # => 32x20
    Thursday, February 28, 13

    View Slide

  43. Accessors Gotcha
    class User
    attr_accessor :name
    attr_reader :permissions
    def initialize(name, *permissions)
    @name, @permissions = name, permissions
    end
    end
    current_user = User.new('jake', 'manage.users', 'manage.assets')]
    puts current_user.permissions.join(', ')
    # => manage.users, manage.assets
    current_user.permissions << 'manage.posts'
    puts current_user.permissions.join(', ')
    # => manage.users, manage.assets, manage.posts
    Thursday, February 28, 13

    View Slide

  44. Methods
    class Asset
    attr_accessor :dimensions, :path
    def initialize(path)
    @path = path
    @dimensions = [32, 20]
    end
    def width
    @dimensions[0]
    end
    def height
    @dimensions[1]
    end
    def square?
    width == height
    end
    end
    image = Asset.new('/images/hello.jpg')
    puts "I'm No Square" unless image.square?
    Thursday, February 28, 13

    View Slide

  45. Public, Protected, Private
    class Asset
    attr_accessor :dimensions, :path
    def initialize(path)
    @path = path
    @dimensions = [32, 20]
    end
    def square?
    width == height
    end
    protected
    def get_type
    # get class type
    end
    end
    image.get_type
    # => protected method `get_type' called for #0x007f9ce2085508> (NoMethodError)
    Thursday, February 28, 13

    View Slide

  46. Inheritance
    class Image < Asset
    def get_mime
    "image/jpeg"
    end
    end
    class Video < Asset
    def fps
    30
    end
    end
    image = Image.new('/images/new.jpg')
    puts image.dimensions.join('x')
    # => 32x20
    puts image.get_mime
    # => “image/jpeg”
    Thursday, February 28, 13

    View Slide

  47. Modules
    Mixins
    7
    Thursday, February 28, 13

    View Slide

  48. Creating Module
    module HTML5
    def render
    asset_type = self.get_type
    "HTML GOES HERE"
    end
    end
    module Taggable
    def slug
    self.title.gsub(' ', '-')
    end
    end
    Thursday, February 28, 13

    View Slide

  49. Namespacing
    module Output
    module HTML5
    def render
    asset_type = self.get_type
    "HTML GOES HERE"
    end
    end
    end
    class Admin::AlbumsController < ApplicationController
    restrict_to_permission :edit_pages, :except => [ :index, :show ]
    before_filter :login_required, :only => [ :index, :show ]
    def users
    ::User.findByAlbum(self.id)
    end
    end Global Namespace
    Thursday, February 28, 13

    View Slide

  50. Applying mixin
    module SumMixin
    def sum
    self.inject(:+)
    end
    end
    [4,3,2,1,5,6].sum
    # => undefined method `sum' for [4, 3, 2, 1, 5, 6]:Array
    (NoMethodError)
    Array.send(:include, SumMixin)
    puts [4,3,2,1,5,6].sum
    # => 21
    Thursday, February 28, 13

    View Slide

  51. Applying mixin
    class Array
    def sum
    self.inject(:+)
    end
    end
    class Array
    include SumMixin
    end
    puts [4,3,2,1,5,6].sum
    # => 21
    Thursday, February 28, 13

    View Slide

  52. Tools/Libs
    8
    Thursday, February 28, 13

    View Slide

  53. {
    frameworks: ['Rails', 'Sinatra'],
    build: 'Rake',
    deployment: 'capistrano',
    library_management: ['gem', 'bundler']
    repl: 'irb',
    versioning: ['rvm', 'rbenv']
    }
    Thursday, February 28, 13

    View Slide

  54. Questions? Concerns?
    Complaints?
    Thursday, February 28, 13

    View Slide

  55. Thanks for Listening
    jakefolio
    http://jakefolio.com
    [email protected]
    Please Rate Me!
    https://joind.in/7946
    Thursday, February 28, 13

    View Slide