Scripting with Ruby What is a scripting language? What is Ruby?

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
A Guide to Unix Using Linux Fourth Edition
Fundamentals of Python: From First Programs Through Data Structures Chapter 2 Software Development, Data Types, and Expressions.
What is a scripting language? What is Python?
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Scripting Languages CS351 – Programming Paradigms.
Scripting Languages. Originally, a script was a file containing a sequence of commands that needed to be executed Control structures were added to make.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.
Chapter 14 © 2009 by Addison Wesley Longman, Inc Origins and Uses of Ruby - Designed by Yukihiro Matsumoto; released in Use spread rapidly.
Introduction to Shell Script Programming
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
1 Perl Perl basics Perl Elements Arrays and Hashes Control statements Operators OOP in Perl.
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Nael Alian Introduction to PHP
Fundamentals of Python: First Programs
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction to PHP Advanced Database System Lab no.1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Introduction to Java Java Translation Program Structure
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
1 Introduction to Perl CIS*2450 Advanced Programming Techniques.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Scripting with Ruby What is a scripting language? What is Ruby?
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CIT 383: Administrative ScriptingSlide #1 CIT 383: Administrative Scripting Regular Expressions.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Scripting with Ruby What is a scripting language? What is Ruby?
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
Introduction to information systems RUBY dr inż. Tomasz Pieciukiewicz.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
PHP using MySQL Database for Web Development (part II)
Web Database Programming Using PHP
Fundamentals of Programming I Overview of Programming
Chapter 6 JavaScript: Introduction to Scripting
Scripting Languages Info derived largely from Programming Language Pragmatics, by Michael Scott.
Ruby: An Introduction Created by Yukihiro Matsumoto in 1993 (named after his birthstone) Pure OO language (even the number 1 is an instance of a class)
Web Database Programming Using PHP
Introduction to Scripting
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
Chapter 14 Introduction to Ruby.
Ruby Testing 2, 11/9/2004.
PHP.
Web DB Programming: PHP
PHP an introduction.
Presentation transcript:

Scripting with Ruby What is a scripting language? What is Ruby?

Scripting Languages Originally, a script was a file containing a sequence of commands that needed to be executed Control structures were added to make it possible to do more with scripts

Characteristics of Scripting Languages Generally interpreted Dynamic typing - no declarations Make text processing easy Often provide pattern matching for strings Provide file and directory manipulation Make it easy to do things quickly

Basic Scripting Languages Unix and Linux come with shell programs which are programmable – sh – bash – ksh – csh DOS had BAT files

Scripting in Other Environments Even with a GUI operating system, it is still useful to be able to automate repetitive tasks – Windows still has bat files – Mac OS has AppleScript Some applications have a scripting language built into them – Microsoft applications have Visual Basic for Applications (VBA) – Hypercard (Apple) had HyperTalk

Other Scripting Languages Other scripting languages were developed to provide increased capability – sed -- adapted from the UNIX ed editor in 1977 – AWK -- created by Ajo, Wienberger and Kernighan in 1977 – Tcl -- an extensible scripting language written by John Ousterhout in 1987 – Perl -- created by Larry Wall in 1986 – Python-- created in 1989 by Guido van Rossum – Ruby -- created in 1993 by Yukihiro Matsumoto

Scripting and the Web More recently, a number of scripting languages have been developed for use with web browsers – PHP – JavaScript – Active Scripting provided scripting for web pages built using the ASP framework

Scripting on onyx the shell languages sed awk perl python ruby tcl javascript php

Ruby object-oriented – everything is an object interpreted "syntax light" variables are dynamically typed – variables are references to objects supports single inheritance – mixins give some of benefits of multiple inheritance

Running a ruby program Run the interpreter by typing ruby prog.rb Make an executable script – The first line should be #!/usr/bin/ruby – Make the file executable and type its name to run it chmod +x prog.rb./prog.rb Use the irb command to get an interactive session

Basic Syntax Program is a sequence of definitions and statements No semicolons at end of statement – semicolons can separate statements on a single line Parentheses around method arguments are optional – unless ambiguous Comments start with a #

Naming Conventions – local variables, parameters and method names start with lower case letter or underscore – class and module names and constants start with an uppercase letter instance variables start class variables start with global variables start with $

Numbers in Ruby Numbers are objects –Integer –Fixnum –Bignum –Float –Complex Ruby distinguishes between integers and floating point types Usual operators plus exponentiation Math class has usual selection of methods Integers can have an arbitrary number of digits

Ranges Ruby has a class that represents subranges 'a'..'z' Can be used as conditions Operations – to_a converts range to array – include? tests for inclusion, also === – min, max – each is iterator – reject is iterator that skips members based on some condition

Representing Text A string is a sequence of characters – Original implementation was a simple array of bytes – Now consists of an array of bytes with an associated encoding No separate type for a single character

Strings Literals can use either single or double quotes – Variables can be interpolated in double-quoted strings "name = #{name}" – Escape characters are replaced in double- quoted strings – Single quoted strings can escape only \ and ' %q and %Q are alternate forms for single and double quoted strings Also, check out here documents

String Operations Strings can be concatenated with + – both operands must be strings << concatenates another type of object * repeats the string a specified number of times == and === compare for equality is like compare to [] indexes an element

String methods split, scan followed by optional regular expression tokenize string capitalize, capitalize!, downcase, downcase! include? index length

Regular Expressions Patterns enclosed between slashes / Operator =~ means matches (returns boolean) Uses perl syntax

Arrays Creating – create with literal – Array.new creates an empty array Size can grow dynamically Slices can be retrieved and assigned Useful methods: concat, collect, delete, each, empty?, join, sort, dup

Hashes Creating – Use a literal h = {'horse' => 'mammal', 'trout' => 'fish', 'frog' => 'amphibian'} – Use new h2 = Hash.new can specify value of missing element as argument to new

Hash Methods default returns the default value (nil by default); can also be set delete, delete_if remove elements replace updates an element each, each_key, each_value iterate through the hash length returns number of entries has_key?, has_value? sort, dup, …

I/O puts prints its argument followed by a newline print does not append a newline printf for formatted output gets reads a line – default destination is $_

Selection if statement if elsif else end if statement modifier if unless statement modifier unless

Repetition while statement while end while statement modifier while until statement modifier until

Conditional Modifiers There are four modifiers that can control whether a single statement gets executed – if – unless – while – until The syntax is

Blocks Chunk of code that can be passed to a method – behaves like an anonymous (unnamed) function – often used with iterators Two forms do end {| | <statements } A method can have a block as a parameter – block is executed by typing yield

Iterators Blocks are used to implement iterators Pseudocode for typical iterator def each for each element yield element end

Methods Return value of a method is the value of the last statement executed – irb prints value of each statement it executes Syntax for defining def fnName end

Classes Attributes are private ) – attr_reader to sepcify accessors – attr_writer to specify mutators Class variables ( ) – methods called with class name private, protected, public access for methods

For more information David Thomas and Andrew Hunt, Programming Ruby, The Pragmatic Programmer's Guide, Addison Wesley Ruby home page