Lecture 1 Introduction
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 1 Introduction 1 / 57 What is this course about? Ruby language web development using Ruby on Rails database design, API usage and integration, complex front-end design (UI/UX) popular software architecture patterns how the web works: internet's client-server model version control (Git) amongst other topics 2 / 57 What does this do? 3.times do print 'Hello, world!' end 3 / 57 Why Ruby? Optimized for programmer happiness Used for Ruby on Rails Very popular web framework 4 / 57 Course Policies 5 / 57 Prerequisites Currently taking CIS 120 or have already taken it (or instructor permission) Basic HTML/CSS knowledge Codecademy Mozilla Developer Network W3Schools Good to know: tag, basic syntax, important tags e.g. <h1> Let us know if you do not have the prerequisites or have any other concerns 6 / 57 CIS 196 Course Structure Lecture Topics: 3 weeks on Ruby programming language 6 weeks on Ruby on Rails web framework 3 weeks on miscellaneous topics 9 homework assignments 1 final project For more details, read the course syllabus 7 / 57 CIS 19x Course Schedule 3 shared lectures during semester 1/23: Command Line 1/30: git/github 2/6: HTML/CSS basics 8 / 57 Grading 70% - Homework 25% - Final Project 5% - In-class polls 9 / 57 Homework Due on Wednesdays at noon through GitHub Classroom 2 free late days to use on any assignment After, 20% will be deducted for each day late We give you all our automated tests Unlimited submissions Your latest submission will be used for grading 10 / 57 Homework Grading Correctness (15 pts) Full credit if you pass all our automated tests on Travis CI Style (5 pts) Full credit if you have no Rubocop offenses Best Practices (5 pts) TAs will manually grade your code to ensure the Ruby Way 11 / 57 Final Project Must be a web app built using Ruby on Rails Must be an idea approved by either of us ahead of time Milestones included to keep you on track Demo to both instructors during Reading Days. Project is due the day of the demo 12 / 57 In-class Participation We'll use Poll Everywhere during class Polls will be scattered throughout lecture You will receive 5% of your grade based on completing polls 13 / 57 Poll Everywhere Get started by going to https://pollev.com/cis196776 When asked to provide a screen name, make it your preferred first name followed by your last name 14 / 57 Academic Integrity Don't copy/paste others' code Don't have mid-level discussions High-Level Discussion What is Rails used for? Mid-Level Discussion How did you do Homework 1? Low-Level Discussion What is the syntax for iterators? 15 / 57 Tentative Oce Hours Weekday Time TA Monday 5-7pm Sanjana Monday 7-9pm Desmond Tuesday 7:30-9:30pm Zhilei Wednesday 7-9pm Zena Thursday 8-10pm Edward Sunday 8-9pm Zena Office hour locations will be announced soon. 16 / 57 Piazza sign up Piazza sign up 17 / 57 Course Websites CIS 196 Website - All relevant course info Piazza sign up - All course discussions & communication GitHub - HW repos and submission Travis CI - Results of tests run on submission Note that we will be using travis-ci.com Canvas - Viewing grades 18 / 57 Ruby 19 / 57 What you'll need Text editor (Sublime Text is probably easiest) Command Line If you use Windows, you must use a Linux VM or dual boot Linux We have provided a VM here with Ruby and Rails pre-installed Ruby 20 / 57 Installing Ruby Use the Ruby Version Manager (RVM) to manage and install Ruby versions Use this even if you plan on just using one version In this class, we will be using version 2.5.1 Make sure you download the correct version 21 / 57 Resources Use the Ruby Docs whenever you get stuck Make sure you're looking at the correct version (2.5.1) This class will follow this style guide 10 points of your homework grade come from style and best practices 5 of which come from Rubocop which is our style checker 22 / 57 History of Ruby Ruby was designed in the 90s by a Japenese programmer, Yukihiro "Matz" Matsumoto Influenced by Perl, Ada and other scripting languages (Matz's favorite languages) Achieved mass acceptance in mid-2000's Much of Ruby's success and growth can be attributed to Rails Read about Matz's inspiration for Ruby here 23 / 57 Running Ruby Use a REPL (Read-Execute-Print-Loop) with the irb command in terminal Allows you to write and execute Ruby code from the Command Line This is great for testing Get out of the REPL by entering quit Execute .rb files with the ruby command: ruby file.rb 24 / 57 Types in Ruby Ruby is strongly typed Every object has a fixed type Ruby is dynamically typed Variables do not need to be initialized Compare to Java which is statically typed Almost everything in Ruby is an object 25 / 57 Printing in Ruby You can print a value with three different commands: print, puts, and p print outputs the value and returns nil puts outputs the value with a newline and returns nil p both outputs and returns the value I'll use p in my examples since it formats output better I will denote output with #=> p 'hello world' #=> "hello world" 26 / 57 Numerics There are several types of numbers in Ruby Unlike Java, numbers are also objects in Ruby The main ones you'll be using are instances of the Integer and Float classes This is helpful to know when looking at the Ruby Docs If you see anything about FixNum or BigNum, note that they were deprecated in Ruby 2.4 27 / 57 Local Variables Since Ruby is dynamically typed, you don't need to declare types (like int, String, etc.) Local variables are assigned to bare words foo = 100 p foo #=> 100 28 / 57 Strings Strings can be denoted with either single or double quotes They can be concatenated together with + and appended with << hello = 'Hello' world = 'world!' foo = hello + ', ' + world p foo #=> "Hello, world!" p hello #=> "Hello" hello << '!' p hello #=> "Hello!" Note that concatenation did not change the variable, but appending did 29 / 57 Escaped Characters only work in Double Quotes print 'hello\nworld' #=> hello\nworld print "hello\nworld" #=> hello world 30 / 57 Only Double Quotes support String Interpolation When inserting variables into strings, string interpolation is preferred Put local variable betwen the curly braces in #{} This calls to_s on the variable foo = 5 p "Hello, #{foo}!" #=> "Hello, 5!" # Compare to: p 'Hello, ' + foo.to_s + '!' #=> "Hello, 5!" 31 / 57 When to use Single Quotes vs. Double Quotes Use double quotes if: You're using escaped characters or string interpolation You want to include single quotes in the string In all other cases, use single quotes It makes for more readable code 32 / 57 Symbols Denoted with a colon in front They are immutable For example, you cannot do :a << :b because that would change :a They are unique The equal? method checks for referential equality 'hi'.equal? 'hi' #=> false :hi.equal? :hi #=> true 33 / 57 Boolean States All Ruby objects have boolean states of either true or false In fact, the only two objects in Ruby that are false are nil and false itself nil is an object that represents nothingness Kind of like Java's null 34 / 57 If Statements Conditionals can be used for control flow and to return different values based on the condition Use the elsif keyword for more branching foo = if true 1 elsif false 2 else 3 end p foo #=> 1 35 / 57 Unless Statements Evaluated when the condition is false Equivalent to if not Note that you should never have an unless statement followed by an else Instead, use an if ... else flow unless false p 'hello' end #=> "hello" 36 / 57 Conditional Modiers if and unless can be placed at the end of the line This helps code read more like English These are prefered for one liners p 'hello' if true #=> "hello" p 'bye' unless false #=> "bye" 37 / 57 Ternary Operator When using one line if-else statements, favor the ternary operator p true ? 'hello' : 'goodbye' #=> "hello" p false ? 'hello' : 'goodbye' #=> "goodbye" 38 / 57 Methods Method signature in the form of def method_name(arg1, arg2, ...) followed by end keyword In method calls, parentheses around arguments can be omitted if unambiguous Our Style Guide has a lot to say about methods & parentheses def hi 'hello, there' end def hello(name) puts "hello, #{name}" end puts hi #=> "hello, there" hello('Matz') #=> "hello, Matz" hello 'DHH' #=> "hello, DHH" 39 / 57 Implicit Returns Methods in Ruby always return exactly one thing They will return the return value from the last executed expression This means that you do not have to explicitly type return x at the end of the method Only use the return keyword when you want to exit a method early 40 / 57 Guard Clauses Instead of wrapping an entire method in a conditional, use a guard clause to exit early # bad # good def foo(bar) def foo(bar) if bar return unless bar p 'hello' p 'hello' p 'goodbye' p 'goodbye' end end end 41 / 57 Method Names While not strictly enforced, Ruby has certain conventions with naming methods Methods are written in snake_case If a method ends in: ?, it should return a boolean (e.g. Array#empty?) =, it should write to a variable (e.g. writer methods) !, it should perform an operation inplace on the object it's being called on (e.g.