Ruby : A Dynamic Object Oriented Scripting Language

Similar documents
15.1 Origins and Uses of Ruby

CHAPTER 3: CORE PROGRAMMING ELEMENTS

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

Getting started with Java

CSC Web Programming. Introduction to JavaScript

Computing with Strings. Learning Outcomes. Python s String Type 9/23/2012

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

CS 5142 Scripting Languages

Alleviate the Apprehension of Coding in Ruby

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Ruby: Introduction, Basics

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Advanced Algorithms and Computational Models (module A)

RUBY VARIABLES, CONSTANTS AND LITERALS

URLs and web servers. Server side basics. URLs and web servers (cont.) URLs and web servers (cont.) Usually when you type a URL in your browser:

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

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Programming in Python 3

Introductory Perl. What is Perl?

Introduction to Programming Using Java (98-388)

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Ruby for Rails by David A. Black Chapter 10

Your First Ruby Script

Multiple Inheritance using modules in Ruby

Language Reference Manual

XQ: An XML Query Language Language Reference Manual

Chapter 10: Creating and Modifying Text Lists Modules

CSCI 4152/6509 Natural Language Processing. Perl Tutorial CSCI 4152/6509. CSCI 4152/6509, Perl Tutorial 1

MTAT Agile Software Development in the Cloud. Lecture 3: Ruby (cont.)

CSE 374 Programming Concepts & Tools. Laura Campbell (thanks to Hal Perkins) Winter 2014 Lecture 6 sed, command-line tools wrapup

Server side basics CS380

Lecture 12. PHP. cp476 PHP

X Language Definition

1. Introduction. 2. Scalar Data

Pointers (continued), arrays and strings

LISTS WITH PYTHON. José M. Garrido Department of Computer Science. May College of Computing and Software Engineering Kennesaw State University

Pointers (continued), arrays and strings

User Commands sed ( 1 )

Simple Java Reference

IT441. Network Services Administration. Perl: File Handles

MR Language Reference Manual. Siyang Dai (sd2694) Jinxiong Tan (jt2649) Zhi Zhang (zz2219) Zeyang Yu (zy2156) Shuai Yuan (sy2420)

Quick Reference Guide

ASPRS LiDAR SPRS Data Exchan LiDAR Data Exchange Format Standard LAS ge Format Standard LAS IIT Kanp IIT Kan ur

CSC 1107: Structured Programming

Comp Exam 1 Overview.

Lecture 4: Simple Input-Calculate-Output Programs

Problems for Op 2015

Indian Institute of Technology Kharagpur. PERL Part III. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

The Typed Racket Reference

Built-in Types of Data

Lecture 4: Basic I/O

Ruby: Introduction, Basics

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Converting File Input

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

PERL Scripting - Course Contents

CSCI-GA Scripting Languages

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Script language: Python Data structures

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

COMP284 Scripting Languages Lecture 15: JavaScript (Part 2) Handouts

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Part III Appendices 165

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Quick Reference Guide

The current topic: Python. Announcements. Python. Python

Shells and Shell Programming

Java Basic Datatypees

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

EL2310 Scientific Programming

Introductory Perl. Boston University Information Services & Technology. Course Coordinator: Timothy Kohl. What is Perl?

Appendix. As a quick reference, here you will find all the metacharacters and their descriptions. Table A-1. Characters

COMP519 Web Programming Lecture 17: Python (Part 1) Handouts

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Scripting Languages. Diana Trandabăț

Operators in java Operator operands.

Shells and Shell Programming

ASML Language Reference Manual

A variable is a name for a location in memory A variable must be declared

CSCI 6610: Review. Chapter 7: Numbers Chapter 8: Characters Chapter 11 Pointers

C Introduction Lesson Outline

Perl Regular Expressions. Perl Patterns. Character Class Shortcuts. Examples of Perl Patterns

ENGR 102 Engineering Lab I - Computation

Arrays (Lists) # or, = ("first string", "2nd string", 123);

Table of Contents Date(s) Title/Topic Page #s. Abstraction

Python source materials

Babu Madhav Institute of Information Technology, UTU 2015

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Common Lisp. Blake McBride

Outline. Data and Operations. Data Types. Integral Types

Introduction to: Computers & Programming: Strings and Other Sequences

Number Systems, Scalar Types, and Input and Output

Ruby. Peter Wilhelmsson

Review. Input, Processing and Output. Review. Review. Designing a Program. Typical Software Development cycle. Bonita Sharif

Peer Instruction 1. Elementary Programming

Transcription:

Ruby : A Dynamic Object Oriented Scripting Language Kuldeep Gharat kuldeep@cse.iitb.ac.in November 29, 2004 Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 1 / 18

Standard Types Overview Ruby, like most other languages provides standard data types like : Numbers Strings Regular Expressions Anchors Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 2 / 18

Numbers Number Classes All numbers are objects of one of the three classes : Fixnum : Integers within a range say from 2 62 to 2 62-1 Bignum : Integers outside the range of Fixnum Float : Floating point numbers Conversions Conversion of an integer from Fixnum class to Bignum class and vice-versa is transparent and done automatically by Ruby. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 3 / 18

Numbers Object Orientedness Numbers are objects of their respective classes. Thus We can find the absolute value of a number by using anumber.abs and not abs(anumber). Floating points are written as 10.0e7 and not 10.e7. 10.e7 tries to call the method e7 of the corresponding class. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 4 / 18

Iterators and Methods to manipulate Integers Iterators There are various iterators available for integers. times : 3.times {print X } will print X X X upto : 1.upto(5) { print i, } will print 1 2 3 4 5 downto : 99.downto(95) { print i, } will print 99 98 97 96 95 step : 50.step(70,5) { print i, } will print 50 55 60 65 70 methods There are methods to convert numbers to integers and vice-versa: to i : str.to i will give integer representation of string str to s : val.to s will give string representation of number val Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 5 / 18

Strings String Class Strings in Ruby are sequences of 8-bit bytes. They are objects of the String class. Strings in Ruby are mostly similar to those in other languages like perl. Substitution in Strings One of the ways of string construction is by sustitution. This is done using the #{expr} sequence. Thus the value of any Ruby expression is substituted into the string. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 6 / 18

Strings String Construction There are 3 ways to construct string literals in Ruby. They are : %q/general single-quoted string/ %Q!general single-quoted string/ Here document Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 7 / 18

Here Document Usage Here Document Usage A Here document consists of lines in the source up to, but not including, the terminating string that must be specified after the << characters. astring = <<END OF STRING The body of the string is the input lines upto one ending with the same text that followed the << END OF STRING Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 8 / 18

String Manipulation Methods to Manipulate Strings Here a few of the numerous(somewhere around 75) string manipulation methods. split method Splits each line into fields delimited by some character. split can be used to extract Date, Month, Year from 10/12/2004 scan method Extracts fields that match a regular expression. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 9 / 18

String Manipulation chomp method Strips off newline character from the end of a line. It is useful when input is read from console. The input lines are stripped of the newline characters at the end. squeeze method Trims runs of repeated characters. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 10 / 18

Ranges Ranges Ranges occur naturally in the real world. Months, integers, etc.. Ruby lets us model these. Ruby models sequences, intervals and conditions using ranges. It uses the range operators..,... (1..10).to a >> results in [1 2 3 4 5 6 7 8 9] ( bar.. bat ).to a >> results in [ bar, bas, bat ] Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 11 / 18

Ranges Methods for ranges Here are a few methods that allow you to iterate over the ranges and test their contents. digits = (1..9) include? : digits.include?(5) min : digits.min max : digits.max reject : digits.reject {i<5} each : digits.each do dial(digit) end Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 12 / 18

Regular Expressions Regexp class All regular expressions are objects of the class Regexp. They are used to match patters against strings. Creating Regular Expressions Regular Expressions can be created using of these : Regexp Constructor : Regexp.new(<regular expression>) Using /pattern/ Using %r{pattern} Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 13 / 18

Matching Regular Expressions Matching Objects to regular expressions We can match an object to a regular expression by using the following match method of Regexp class Match operators (positive match) and (negative match) Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 14 / 18

Anchors Anchors Regular expressions match patterns at the beginning of the string only. Anchors match a regular expression at the beginning or at the end. ^ matches the pattern at the beginning of a line only. $ matches a pattern at the end of the line only. \A matches a pattern at the beginning of a string. \Z matches a pattern at the end of a string. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 15 / 18

Anchors Examples Examples str = "Hello World!\n This is IIT Bombay" /^Hello/ : matches Hello at the beginning of str /Bombay$/ : matches Bombay at the end of the str /^ This/ : matches the word This in the middle of str. /\AThis/ : does not match This as it is in the middle of str. /\AHello/: matches Hello because it is in the beginning of the string. Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 16 / 18

References http://www.ruby-doc.org/ http://kylecordes.com/files/introtoruby.ppt http://pragmaticprogrammer.com/ http://www.pragmaticprogrammer.com/talks/perlmongers Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 17 / 18

Thank you Kuldeep Gharat kuldeep@cse.iitb.ac.in () Ruby : A Dynamic Object Oriented Scripting Language November 29, 2004 18 / 18