Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby.new

E4df4a30f000a225df7114e4940944e7?s=47 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.

E4df4a30f000a225df7114e4940944e7?s=128

jakefolio

February 28, 2013
Tweet

Transcript

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

  2. Thursday, February 28, 13

  3. Thursday, February 28, 13

  4. Thursday, February 28, 13

  5. Thursday, February 28, 13

  6. Thursday, February 28, 13

  7. jakefolio http://jakefolio.com me@jakefolio.com Jake Smith Thursday, February 28, 13

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

  9. History 1 Thursday, February 28, 13

  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
  11. Ruby 2 Thursday, February 28, 13

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

  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
  14. Variables foo_bar # => Local Variable Foobar # => Constant

    @foo_bar # => Instance Variable @@foo_bar # => Class Variable $foo_bar # => Global Variable Thursday, February 28, 13
  15. Conventions Thursday, February 28, 13

  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
  17. Data Types 3 Thursday, February 28, 13

  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 = <<END 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
  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
  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
  21. Number (128 / 10).to_f # => 12.0 128.0 / 10

    # => 12.8 128.to_f / 10 # => 12.8 Thursday, February 28, 13
  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
  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 # => #<MatchData "jsmith92"> 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
  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
  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
  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
  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
  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
  29. Conditionals and Loops 4 Thursday, February 28, 13

  30. Conditionals if @post.published? and @post.approved? # Show the post else

    # DO NOT show post end unless @user.access? # Access Denied end if !@user.access? # 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
  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
  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
  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
  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
  35. Blocks, Procs, and Lambdas 5 Thursday, February 28, 13

  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
  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
  38. Block Proc Lambda Thursday, February 28, 13

  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
  40. Classes 6 Thursday, February 28, 13

  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
  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
  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
  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
  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 #<Asset: 0x007f9ce2085508> (NoMethodError) Thursday, February 28, 13
  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
  47. Modules Mixins 7 Thursday, February 28, 13

  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
  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
  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
  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
  52. Tools/Libs 8 Thursday, February 28, 13

  53. { frameworks: ['Rails', 'Sinatra'], build: 'Rake', deployment: 'capistrano', library_management: ['gem',

    'bundler'] repl: 'irb', versioning: ['rvm', 'rbenv'] } Thursday, February 28, 13
  54. Questions? Concerns? Complaints? Thursday, February 28, 13

  55. Thanks for Listening jakefolio http://jakefolio.com me@jakefolio.com Please Rate Me! https://joind.in/7946

    Thursday, February 28, 13