Productivity Ruby on Rails • Web Framework • Launched Ruby into the public eye • Inspiration for many other frameworks RhoMobile • Uses Ruby to bridge devices • Rhodes framework takes cues from Rails
single or double quoted. • As well as more esoteric syntaxes: %q|Hello World| String Interpolation • “There are #{24 * 7} hours in a week.” • Only works with double quotes.
No need to specify type • Can redefine at any time • a = 15 Strong Dynamic Typing • Strong: variable always has an explicit type • Dynamic: type can change at any time
No need to specify type • Can redefine at any time • a = 15 Strong Dynamic Typing • Strong: variable always has an explicit type • Dynamic: type can change at any time Duck Typing • def my_addition(value1, value2) value1 + value2 end • my_addition(“Hello”, “World”) # => “HelloWorld”
r = /\d\d\d\d-\d\d-\d\d/ Match Strings • info = “Conference runs from 2012-06-04 to 2012-06-06” • Use =~ to find match location • info =~ /\d\d\d\d-\d\d-\d\d/ # => 21 • Use match to get the data back • info.match(/\d\d\d\d-\d\d-\d\d/) # => #<MatchData "2012-06-04">
r = /\d\d\d\d-\d\d-\d\d/ Match Strings • info = “Conference runs from 2012-06-04 to 2012-06-06” • Use =~ to find match location • info =~ /\d\d\d\d-\d\d-\d\d/ # => 21 • Use match to get the data back • info.match(/\d\d\d\d-\d\d-\d\d/) # => #<MatchData "2012-06-04"> Substitutions • info.sub(/\d\d\d\d-\d\d-\d\d/, “today”) - #=> “Conference runs from today to 2012-06-06” • info.gsub(/\d\d\d\d-\d\d-\d\d/, “today”) - #=> “Conference runs from today to today”
exit when “pause” sleep 20 when String puts “The input is #{input}” when 5..10 puts “The input was between 5 and 10” when /\d\d\d\d-\d\d-\d\d/ puts “The input looks like a date” else puts “I don’t understand you.” end
count < 10 • puts count • count += 1 • end += • Equivalent to count = count + 1 • Can be used with any number • And all basic math operations (*=, -=, etc.)
count < 10 • puts count • count += 1 • end += • Equivalent to count = count + 1 • Can be used with any number • And all basic math operations (*=, -=, etc.) Inline while • count = 0 • puts count += 1 while count < 10
puts “I love #{fruit}” • end Use do & end for multi-line, {} for single-line • fruits.each {|fruit| puts “I love #{fruit}” } Same syntax works for other forms of iterating • .map • .each_with_index • .each_key
NameError: undefined local variable or method `my_method' for #<Class: 0x007fef2bc51ee0> from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/ activerecord-3.2.2/lib/active_record/dynamic_matchers.rb:50:in `method_missing' from /Users/daniel/Projects/my_project/app/models/sign.rb:16:in `import' from (irb):4 from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@project/gems/railties-3.2.2/lib/ rails/commands/console.rb:47:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands/console.rb:8:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
NameError: undefined local variable or method `my_method' for #<Class: 0x007fef2bc51ee0> from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/ activerecord-3.2.2/lib/active_record/dynamic_matchers.rb:50:in `method_missing' from /Users/daniel/Projects/my_project/app/models/sign.rb:16:in `import' from (irb):4 from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@project/gems/railties-3.2.2/lib/ rails/commands/console.rb:47:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands/console.rb:8:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
NameError: undefined local variable or method `my_method' for #<Class: 0x007fef2bc51ee0> from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/ activerecord-3.2.2/lib/active_record/dynamic_matchers.rb:50:in `method_missing' from /Users/daniel/Projects/my_project/app/models/sign.rb:16:in `import' from (irb):4 from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@project/gems/railties-3.2.2/lib/ rails/commands/console.rb:47:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands/console.rb:8:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@ project/gems/railties-3.2.2/lib/ rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
NoMethodError: undefined method `first_name' for nil:NilClass from (irb):1 from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands/console.rb:47:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands/console.rb:8:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
NoMethodError: undefined method `first_name' for nil:NilClass from (irb):1 from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands/console.rb:47:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands/console.rb:8:in `start' from /Users/daniel/.rvm/gems/ruby-1.9.3-p194@signwars/gems/railties-3.2.2/ lib/rails/commands.rb:41:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
This Presentation • https://speakerdeck.com/u/danielmorrison • or shorter URL: http://bit.ly/LqCinu Talk to Me • @danielmorrison • http://collectiveidea.com