Ruby Presentation - Handout

215 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
215
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby Presentation - Handout

  1. 1. The Language ToolsRuby: Why We Love Ithttps://github.com/Kelsin/ruby-presentationChristopher GiroirNovember 8th, 2011Christopher Giroir Ruby: Why We Love ItThe Language Tools1 The LanguageBasicsWhy We Love ItGems2 ToolsBundlerRVMChristopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsOverviewInspired by Smalltalk (which I love)Also draws from Perl, Eiffel, Ada and LISPIncludes a REPLBuilt for developers as a language they would love to useDynamic, strict, reflective, object orientedEverything is an expression (even statements)Everything is executed imperatively (even declarations)Christopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsObject OrientedEverything is an objectSingle InheritanceModules can be mixed inDynamic DispatchChristopher Giroir Ruby: Why We Love It
  2. 2. The Language ToolsBasicsSimple Code1 5.times { print "Hello" }This outputs:1 Hello2 Hello3 Hello4 Hello5 Hello6 => 5Christopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsTypes1 # Strings2 s = ’Testing’34 # Interpreted Strings5 t = "Double #{str}"67 # Symbols8 sym = :chris910 # Arrays11 a = [1,2,3]1213 # Hashes14 h = { :key => ’value’, :chris => ’awesome’ }Christopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsClasses1 class Box2 def initialize(w,h,d)3 @width = w4 @height = h5 @depth = d6 end78 def volume9 @width * @height * @depth10 end11 end1213 box = Box.new(2,2,2)14 box.volume # => 8Christopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsSimple Inheritance1 class JackInTheBox < Box2 def initialize(msg)3 @msg = msg4 super(3,3,3)5 end67 def open8 puts @msg9 end10 end1112 jbox = JackInTheBox.new(’Surprise!’)13 jbox.volume # => 2714 jbox.open # prints ’Surprise!’Christopher Giroir Ruby: Why We Love It
  3. 3. The Language ToolsBasicsControl1 while true == false2 if var == 53 break4 end56 begin7 var - 18 end while var < 4910 next if var == 611 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsBlocks1 [1,2,3].each { |n| puts n }This outputs:1 12 23 34 => [1,2,3]Christopher Giroir Ruby: Why We Love ItThe Language ToolsBasicsBlock Syntax1 5.upto(10) { |n| puts n }This is exactly the same as the following:1 5.upto(10) do |n|2 puts n3 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsWhy We Love ItAttribute Methods1 class Person2 def name3 @name4 end5 def social=(s)6 @social = s7 end8 def age9 @age10 end11 def age=(a)12 @age = a13 end14 endChristopher Giroir Ruby: Why We Love It
  4. 4. The Language ToolsWhy We Love ItThe Easy Way1 class Person2 attr_reader :name3 attr_writer :social4 attr_accessor :age5 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsWhy We Love ItThe Easy Way Explained1 class Person2 attr_reader :name3 attr_writer :social4 attr_accessor :age5 endRuby syntax allows method calls without ()Result is clean and looks like a language featureChristopher Giroir Ruby: Why We Love ItThe Language ToolsWhy We Love ItWe can implement this ourselvesUntested code, please do not copy:1 class Object2 def self.attr_reader(var)3 class_eval <<-METHODS4 def #{var}5 @#{var}6 end7 METHODS8 end9 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsWhy We Love ItWhy Blocks1 (map (lambda (n)2 (+ n 5))3 ’(1 2 3))Becomes:1 [1,2,3].map do |n|2 n + 53 endResults in:1 => [6,7,8]Christopher Giroir Ruby: Why We Love It
  5. 5. The Language ToolsGemsModules1 module Voice2 def say(msg)3 puts msg4 end5 end67 class Person8 include Voice9 end1011 p = Person.new12 p.say(’Hello’) # prints ’Hello’Christopher Giroir Ruby: Why We Love ItThe Language ToolsGemsUsing GemsRequire loads in files1 require ’saver’ # pulls in ’saver.rb’Gems allow us to not deal with paths1 require ’rubygems’2 require ’saver’34 class Item5 include Saver6 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsGemsWriting Gems1 Gem::Specification.new do |s|2 s.name = "saver"3 s.version = Saver::VERSION4 s.authors = ["Christopher Giroir"]5 s.email = ["kelsin@valefor.com"]6 s.homepage = "http://kelsin.github.com/saver/"78 s.files = ‘git ls-files‘.split("n")9 s.require_paths = ["lib"]1011 s.add_dependency ’activesupport’, ’~> 3.0.0’12 s.add_dependency ’mongo_mapper’13 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsBundlerWhy Bundler?Many projects (i.e. rails apps) are not gems themselvesThey do have gem dependenciesEasy way to install and keep track of these dependenciesMaking sure ONLY the proper gems are usedChristopher Giroir Ruby: Why We Love It
  6. 6. The Language ToolsBundlerThe Gemfile1 source ’http://tools1.savewave.com/rubygems’2 source ’http://rubygems.org’34 gem ’rails’, ’3.0.7’56 gem ’sw-model’, ’0.13.0’78 group :development, :test do9 gem "rspec"10 endChristopher Giroir Ruby: Why We Love ItThe Language ToolsBundlerUsing Bundler1 # Install the gems from the Gemfile2 bundle install34 # Update gems to new versions5 bundle update67 # Execute command with proper gems8 bundle exec rake specIn your ruby code1 require "rubygems"2 require "bundler/setup"3 require "saver"Christopher Giroir Ruby: Why We Love ItThe Language ToolsBundlerGemfile.lockWhen you initially install versions are saved to Gemfile.lockAfter they are only updated on bundle updateSHOULD be checked into version controlProtects from version updatesChristopher Giroir Ruby: Why We Love ItThe Language ToolsRVMWhy RVM?Different projects might use different versions of railsDifferent projects might use different ruby interpretersRubyJRubyRubinusWhile bundler helps, complete gem isolation is better!It’s nice to keep your system ruby separate and not update itChristopher Giroir Ruby: Why We Love It
  7. 7. The Language ToolsRVMUsing RVM1 # Install the default 1.9.2 ruby interpretor2 rvm install 1.9.234 # Switch to using 1.9.25 rvm use 1.9.267 # List installed rubies8 rvm listChristopher Giroir Ruby: Why We Love ItThe Language ToolsRVMRVM Gemsets1 # Create a new gemset2 rvm gemset create savingstar-web34 # List gemsets5 rvm gemset list67 # Switch to a ruby and gemset together8 rvm use 1.9.2@savingstar-webChristopher Giroir Ruby: Why We Love ItThe Language ToolsRVM.rvmrcA .rvmrc file per project allows you to say which ruby andgemset to useShould be in source control. Helps RVM users out, ignored forothersIt’s a shell script that’s executed everytime you cd (veryunsafe)Makes life very easy however!1 rvm use 1.9.2@saveingstar-web --createChristopher Giroir Ruby: Why We Love It

×