CS 390 Software Engineering Lecture 6 Ruby

Similar documents
Overview of the Ruby Language. By Ron Haley

CS 5142 Scripting Languages

Introduction. CSC 440: Software Engineering Slide #1

Lecture 2. Object Orientation

CSE 341, Autumn 2015, Ruby Introduction Summary

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Ruby on Rails for PHP and Java Developers

Lecture 1. Basic Ruby 1 / 61

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Ruby : A Dynamic Object Oriented Scripting Language

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

Lecture 2. Object Orientation 1 / 50

CMSC 330: Organization of Programming Languages. Administrivia

! Broaden your language horizons. ! Study how languages are implemented. ! Study how languages are described / specified

Computer Science Seminar. Whats the next big thing? Ruby? Python? Neither?

PREPARING FOR PRELIM 1

Ruby assign variable if nil. Ruby assign variable if nil.zip

Introduction to Ruby. SWEN-250 Personal Software Engineering

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

Lecture 2. Object Orientation 1 / 51

Introduction CMSC 330: Organization of Programming Languages. Books on Ruby. Applications of Scripting Languages

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Ruby Containers, Blocks, and Procs

Ruby AN OVERVIEW. Luigi De Russis Dipartimento di Automatica e Informatica Politecnico di Torino

CS1 Lecture 3 Jan. 22, 2018

Programming Paradigms

CS 390 Software Engineering Lecture 5 More Git

Ruby Topic Maps. Introduction to Ruby. Benjamin Bock.

Software Engineering 2 (SWT2) Chapter 2: Introduction into Ruby on Rails

Installation Download and installation instructions can be found at

Object Model Comparisons

Your First Ruby Script

Java Applets, etc. Instructor: Dmitri A. Gusev. Fall Lecture 25, December 5, CS 502: Computers and Communications Technology

Conditionals & Control Flow

Lecture 18 Tao Wang 1

CSE 341: Programming Languages

Ruby on Rails. SITC Workshop Series American University of Nigeria FALL 2017

CS 320 Introduction to Software Engineering Spring February 06, 2017

Array variable in class ruby. Array variable in class ruby.zip

Assignment 1 due Monday at 11:59pm

Having Fun with Social Coding. Sean Handley. February 25, 2010

61A Lecture 2. Friday, August 28, 2015

Lecture 1. basic Python programs, defining functions

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

Ruby, with foxes. Ruby, cont d. Warning. why's (poignant) Guide to Ruby

Immutability and Design Patterns in Ruby

Object Oriented Programming in Python 3

Ruby on Rails TKK, Otto Hilska

CS1 Lecture 3 Jan. 18, 2019

Lecture 4. Defining Functions

Dynamically-typed Languages. David Miller

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

COMP519 Web Programming Lecture 12: JavaScript (Part 3) Handouts

Day 2: 19/April/2012 Installing Aptana IDE; Integrated Development Environment And Rails

61A Lecture 2. Wednesday, September 4, 2013

CS 390 Software Engineering Lecture 3 Configuration Management

Ruby Primer and Review for Developers

LibSerial Documentation

CHAPTER 3: CORE PROGRAMMING ELEMENTS

Pairs and Lists. (cons 1 2) 1 2. (cons 2 nil) 2 nil. Not a well-formed list! 1 > (cdr x) 2 > (cons 1 (cons 2 (cons 3 (cons 4 nil)))) ( ) (Demo)

SCHEME AND CALCULATOR 5b

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 1 : Introduction. Department of Computer Science University of Arizona

At the Forge. What Is Ruby? Getting Started with Ruby. Reuven M. Lerner. Abstract

Boldface numbers indicate illustrations, code listings, and tables.

Final thoughts on functions F E B 2 5 T H

Chapter 10 Introduction to Classes

COMP1730/COMP6730 Programming for Scientists. Functional abstraction, with robots

CP215 Application Design

COMP1730/COMP6730 Programming for Scientists. Functions

Chapter 4 Defining Classes I

CS 390 Software Engineering Lecture 4 - Git

What is Ruby? CSc 372. Comparative Programming Languages. 27 : Ruby Introduction. Department of Computer Science University of Arizona

Python Programming Exercises 1

Introduction to Ruby on Rails

Lecture 4. Defining Functions

CMSC 330: Organization of Programming Languages

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Course Goal CMSC 330: Organization of Programming Languages. All Languages Are (Kind of) Equivalent. Studying Programming Languages.

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Introduction to Python. Didzis Gosko

CS Introduction to Computational and Data Science. Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2017

CS1 Lecture 4 Jan. 24, 2018

JavaScript: Introduction, Types

Intro. Scheme Basics. scm> 5 5. scm>

Intro to Ruby Scripting

Configuring Condor with Wallaby. William C. Benton and Robert H. Rati Red Hat, Inc.

Ruby: Introduction, Basics

Imperative vs. objectoriented

Strings and Variables

RUBY RUBY BY E X A MPLE


S206E Lecture 19, 5/24/2016, Python an overview

This exam is open book. Each question is worth 3 points.

CS 3813/718 Fall Python Programming. Professor Liang Huang.

This tutorial has been prepared for beginners to help them understand the basic to advanced concepts related to Ruby Scripting languages.

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Transcription:

CS 390 Software Engineering Lecture 6 Ruby References: Ruby in Twenty Minutes, available at https://www.ruby-lang.org/en/documentation/quickstart/.

Outline Clone Ruby example git repository $ git clone ssh://[user@]csserver.evansville.edu/usr/local/git/ruby-examples.git Install RVM (Ruby Version Manager). Use to install consist version of Ruby and Rails See rvm-installation file Ruby 2

Ruby Ruby is a scripting language, not unlike Python or PHP. Ruby can be run interactively using the irb (Interactive Ruby) interpreter. It evaluates expressions and prints out results. (puts is a command that outputs a string.) irb(main):001:0> "Hello World" => "Hello World" irb(main):002:0> puts "Hello World" Hello World irb(main):003:0> 3+2 => 5 irb(main):004:0> Math.sqrt(9) => 3.0 3

Functions Ruby functions are defined using reserved word def and end with reserved word end. irb(main):010:0> def hi irb(main):011:1> puts "Hello World!" irb(main):012:1> end => :hi Functions may be called with or without parentheses. E.g., using hi or hi(). 4

Parameters Ruby parameters are listed in parentheses. They are referenced using #{<param>} irb(main):015:0> def hi(name) irb(main):016:1> puts "Hello #{name}!" irb(main):017:1> end => :hi irb(main):018:0> hi("matz") Hello Matz! 5

Parameters Ruby parameters can have default values. Functions with parameters may be called without parentheses as long as it is obvious what is being done. irb(main):019:0> def hi(name = "World") irb(main):020:1> puts "Hello #{name.capitalize}!" irb(main):021:1> end => :hi irb(main):022:0> hi "chris" Hello Chris! irb(main):023:0> hi Hello World! 6

Classes Object types are defined using reserved word class and end with reserve word end. Data attribute names are prefixed with @ and are private. irb(main):024:0> class Greeter irb(main):025:1> irb(main):026:2> irb(main):027:2> irb(main):028:1> irb(main):029:2> irb(main):030:2> irb(main):031:1> irb(main):032:2> irb(main):033:2> irb(main):034:1> end => :say_bye def initialize(name = "World") end @name = name def say_hi end puts "Hi #{@name}!" def say_bye end puts "Bye #{@name}, come back soon." 7

Instantiation Ruby objects are instantiated using reserved method new that is a class method that calls the class' initialize method. irb(main):035:0> greeter = Greeter.new("Pat") => #<Greeter:0x16cac @name="pat"> irb(main):036:0> greeter.say_hi Hi Pat! irb(main):037:0> greeter.say_bye Bye Pat, come back soon. 8

Attribute accessors Attributes can be made accessible using attr_accessor. Ruby classes can be opened after definition and new methods and accessors can be added. irb(main):044:0> class Greeter irb(main):045:1> irb(main):046:1> end => Nil attr_accessor :name irb(main):047:0> greeter = Greeter.new("Andy") => #<Greeter:0x3c9b0 @name="andy"> irb(main):050:0> greeter.say_hi Hi Andy! irb(main):051:0> greeter.name="betty" => "Betty" irb(main):053:0> greeter.name => "Betty" irb(main):054:0> greeter.say_hi Hi Betty! 9

Ruby scripts Look at ri20min.rb. First line is #!/usr/bin/env ruby Comments start with # to the end of the line Start of "main program" is if FILE == $0 to the reserved word end. FILE is an environment variable set to the name of this file. $0 is the first command-line argument (i.e. argv[0]). If they are the same, then this file is being run as a program. Otherwise it is being used as a library and this code is ignored. Run script using: ruby ri20min.rb 10

Ruby objects A Ruby object has Smalltalk-like behavior and can be asked if it responds to a particular method name. if @names.respond_to?("each") E.g., this can be used to distinguish between single objects and collection objects. Collections have many methods such as each and join. 11

Arrays Arrays can be defined explicitly giving a list in [ ]. mg.names = ["Albert", "Brenda", "Charles", "Dave", "Engelbert"] 12