1 / 25

Introduction to Ruby

Introduction to Ruby. Introduction. Ruby Basics Ruby for the Web Ruby on Rails Demo Questions. What is Ruby?. Invented in mid-1990s by Yukihiro “ Matz ” Matsumoto Interpreted Language Completely Object Oriented Built to be FUN! (Play with it at http://repl.it/languages/Ruby).

derora
Download Presentation

Introduction to Ruby

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. Introduction to Ruby

  2. Introduction • Ruby Basics • Ruby for the Web • Ruby on Rails • Demo • Questions

  3. What is Ruby? • Invented in mid-1990s by Yukihiro “Matz” Matsumoto • Interpreted Language • Completely Object Oriented • Built to be FUN! (Play with it at http://repl.it/languages/Ruby)

  4. Variables & Strings • Local variables don’t require definition. • Anything in a string surrounded by #{} allows for what’s in the brackets to be evaluated before being added to the string. This is string interpolation.

  5. Conditionals and comparisons

  6. Arrays • Arrays are objects like everything else. • All their functions can be seen online.

  7. Hash • Like ‘associative arrays’ for PHP. Stores a value with a key.

  8. Loops • For loops aren’t commonly used. #each and #times provides a more readable alternative • The variable in the pipes is the current value it’s using. This is block syntax – you’ll see it more.

  9. Iteration • This is how most iteration is done. #each will run what’s between do/end as many times as there are elements, setting the variable in the brackets to the current element.

  10. Object Oriented • EVERYTHING in Ruby is an object. (even classes) • All classes are open to redefinition – even core!

  11. Defining a New Class • All class names must be CamelCased.

  12. Extendibility • Ruby’s greatest strength is extendibility. • There are “gems” (plugins) available for anything you could think of needing. • Install the gem, require it in your code, and you get all its functionality! • 69,880 gems made since July 2009. (http://rubygems.org)

  13. Ruby on the Web • Ruby was just a scripting language. • Programmers wrote frameworks to write Ruby for the web (because it’s fun!) • Popular frameworks include Ruby on Rails and Sinatra.

  14. Ruby on Rails • Framework written by David Heinemeier Hansson and released in 2004. • Open source project (http://github.com/rails/rails) • Written to very rapidly create database backed applications. • Very opinionated – but these opinions have been built by fantastic engineers.

  15. Making a Rails Application Run the below commands in a terminal when Ruby is installed. • gem install rails • rails new blog • cd blog • rails server (a server is built into Rails for development) • Visit http://localhost:3000 • Voila – a web server running on ruby!

  16. Rails – (M)odel-(V)iew-(C)ontroller • Rails is an MVC framework – meaning it separates application logic into three parts, the Model, the View, and the Controller

  17. Rails – Models • Models are object representations of items stored in the database. • There is usually one per “thing” – a table is a model, a chair is a model, etc. • Best practice is to store information here and do nothing else – there shouldn’t be a lot of logic in the model.

  18. Rails – Controllers • Controllers control the interaction between all aspects of the application and decides which view to show. • Demo – rails g controller welcome index • This creates a “welcome” controller with the index action.

  19. Rails - Routes • Routes decide which URL paths navigate to which controllers. • Root :to => “welcome#index” says that the root path goes to the welcome controller, index action.

  20. Rails - Views • Views contain the HTML that get rendered. • The index action of the welcome controller with render the HTML in app/views/welcome/index.html.erb.

  21. Rails - Scaffolding • Rapidly create the common Create/Read/Update/Modify views for a database item. • Rails generate scaffold Post title:string description:text • Rake db:migrate (This updates the database)

  22. Scaffolding – Part 2 • Scaffolding is usually frowned upon – it builds a lot of framework for actions you may not need. • However, for now, it creates a lot for little – feel free to play with it!

  23. Pros/Cons of Rails • Pros • Very fast development • Amazing toolsets – some of the best testing and debugging tools available to web developers. • Cons • Hardware. Ruby and Ruby on Rails make an important decision – developer time is worth more than the cost of hardware.

  24. Questions?

  25. Contact Me Get a hold of me if you’d like to talk! Email: trey.terrell@oregonstate.edu Github: terrellt Check out our source code: http://github.com/osulp

More Related