1 / 21

Rails Week 4

Rails Week 4. Ruby Basics by Brendan Kemp — Based on RailsBridge. Housekeeping. Attendance Website: umassrailscourse.wordpress.com Final Project. Ruby Basics Recap. Ruby. Dynamically typed Object oriented Regular Expressions Blocks & lambdas. Time to Play Along.

delta
Download Presentation

Rails Week 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rails Week 4 • Ruby Basics • by Brendan Kemp — Based on RailsBridge

  2. Housekeeping • Attendance • Website: umassrailscourse.wordpress.com • Final Project

  3. Ruby Basics Recap

  4. Ruby • Dynamically typed • Object oriented • Regular Expressions • Blocks & lambdas

  5. Time to Play Along • IRB — Interactive Ruby Console • Go into Terminal.app and type ‘which irb’ • Type ‘irb’ and get the console • Type ‘ruby ’ + ‘on ’ + ‘rails’ <enter> • What else can you do?

  6. Everything is an Object • Type “test”.upcase • Try “test”.methods • 4.methods • “test”.methods && 4.methods

  7. Methods are Messages • Smalltalk style • Ideally it means that you can send messages without worrying if the method exists • Try: ‘what?’.send(‘upcase’) • 2.+2 • ‘what’.respond_to?(‘+’)

  8. Parentheses are Optional • ‘WHAT?’.send’downcase’ • ‘WHAT?’.send ‘downcase’ • When do you actually use parentheses?

  9. ‘#’ starts a comment • 4.send ‘plus’ 4 # adds 4 and 4 • You don’t need semicolons

  10. Method Conventions • Methods ending in ‘?’ return a boolean. • 4.kind_of?(Integer) • Methods ending in ‘!’ change the object value • @what = ‘ what’ • @what.lstrip! • @what

  11. Symbols • :sym is a unique object in Ruby • :sym.object_id • :sym.object_id • ‘string’.object_id • ‘string’.object_id

  12. Arrays • Arrays are sized dynamically, and can be of mixed type. • a = [1, 2, 3, 4] • a.push “Tell me that you love me more” • a # ???

  13. Hashes • Hashes are associative maps. • states = {“MA” => “Massachusetts”, “Mass” => “Massachusetts”} • The key has to be unique • hashes = {:keys => ‘there is one key’, :keys => ‘there is still one key’}

  14. Strings • :sym.to_s • “string” vs ‘string’ • String interpolation • “string #{@ruby_code} string” • “ ‘word’ is an object of class • #{ ‘word’.class}”

  15. Iteration & Blocks • animals = [“cat”, “dog”, “frog”] • animals.each do |animal| • print “This is a #{animal.capitalize}.” • end

  16. Define a method that takes a block • class Array • def method_that_accepts_block • yield • end • def each_third • self.each_with_index do |array_object, i| • yield if (i % 3) == 0 • end • end • end

  17. Iteration for Hashes • animal_info = {:species => ‘cat’, :color => ‘tabby’, :age => 2} • animal_info.each do |key, value| • “My #{key} is #{value}” • end

  18. Classes • class Animal • def animal_that_kills_me #object method • unless self.name == ‘Bear’ • ‘Shark’ • end • end • def self.most_deadly_animal #class mthd • ‘Bear’ • end • end

  19. Variables & Constants • local_variable • method • @instance_variable • CONSTANT

  20. Review of Relational Databases

  21. Relational Databases • Databases, Tables, Rows, Attributes • Primary Keys • Associations • has_many • belongs_to • has_and_belong_to_many • self-referential joins

More Related