Automation: Time to learn Ruby

Similar documents
Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

15.1 Origins and Uses of Ruby

Introduction to Ruby. SWEN-250 Personal Software Engineering

CS 5142 Scripting Languages

Ruby: Introduction, Basics

Ruby: Introduction, Basics

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

Your First Ruby Script

Installation Download and installation instructions can be found at

Lecture 1. Basic Ruby 1 / 61

Ruby: Introduction, Basics

CSE 341: Programming Languages

CSE 341, Autumn 2015, Ruby Introduction Summary

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

Intro to Ruby Scripting

A Tour of Ruby.... for Java programmers

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

CMSC 331 Final Exam Fall 2013

Ruby on Rails TKK, Otto Hilska

Overview of the Ruby Language. By Ron Haley

CS Unix Tools & Scripting

Ruby Primer and Review for Developers

Pathologically Eclectic Rubbish Lister

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

Ruby on Rails for PHP and Java Developers

CSC 443: Web Programming

Distributed Internet Applications - DIA. Servers and Sockets

What is Scripting? CSCI: 4500/6500 Programming Languages. Higher-level Programming. Origin of Scripting Languages. Contemporary Scripting Languages

Babu Madhav Institute of Information Technology, UTU 2015

Using EnScript to Make Your Life Easier Session 1. Suzanne Widup, James Habben, Bill Taroli

Working with Unix Processes. Copyright 2012 Jesse Storimer. All rights reserved. This ebook is licensed for individual use only.

DOWNLOAD : THE RUBY PROGRAMMING LANGUAGE

How To Create Your Own Freaking Awesome Programming Language

bash, part 3 Chris GauthierDickey

CMSC 330: Organization of Programming Languages. Administrivia

Table of Contents EVALUATION COPY

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Programming Paradigms

Introduction to Perl. c Sanjiv K. Bhatia. Department of Mathematics & Computer Science University of Missouri St. Louis St.

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

PESIT Bangalore South Campus

Perl and Python ESA 2007/2008. Eelco Schatborn 27 September 2007

Scala : an LLVM-targeted Scala compiler

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm Solution

Software Development. Integrated Software Environment

Introduction to Programming: Variables and Objects. HORT Lecture 7 Instructor: Kranthi Varala

Outline. CS3157: Advanced Programming. Feedback from last class. Last plug

1. Introduction. 2. Scalar Data

Regular expressions and case insensitivity

Review of Fundamentals

Lecture 3 Tonight we dine in shell. Hands-On Unix System Administration DeCal

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

(Refer Slide Time: 01:12)

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

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

Emacs manual:

Chapter 1 - Introduction. September 8, 2016

Lab 4. Out: Friday, February 25th, 2005

printf( Please enter another number: ); scanf( %d, &num2);

CSc 372 Comparative Programming Languages

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

Ruby Topic Maps. Introduction to Ruby. Benjamin Bock.

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

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

CSCI-GA Scripting Languages

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

Unit E Step-by-Step: Programming with Python

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

CSC209 Review. Yeah! We made it!

Scripting Languages Perl Basics. Course: Hebrew University

Ruby Objects & Common Methods

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Object Model Comparisons

CS 25200: Systems Programming. Lecture 10: Shell Scripting in Bash

Introduction to Programming

No variable declara<ons. sum = 0! i = 1! while i <= 10 do! sum += i*i! i = i + 1! end! print "Sum of squares is #{sum}\n"!

PERL Scripting - Course Contents

Perl. Interview Questions and Answers

CSCI-GA Scripting Languages

SYMBOLS. you would not assign a value to a symbol: :name = "Fred" The Book of Ruby 2011 by Huw Collingbourne

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Hello, World and Variables

Principles of Programming Languages. Objective-C. Joris Kluivers

And Parallelism. Parallelism in Prolog. OR Parallelism

Scripting Languages. Slides of a talk by Kathleen Fisher (AT&T Research) Stanford Univ.

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Section 1: Tools. Contents CS162. January 19, Make More details about Make Git Commands to know... 3

Algorithms and Programming I. Lecture#12 Spring 2015

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

Alleviate the Apprehension of Coding in Ruby

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

CSE 391 Lecture 5. Intro to shell scripting

An Introduction to Python (TEJ3M & TEJ4M)


CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Programming with Python

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

Ruby: Blocks, Hashes, and Symbols

Transcription:

Automation: Time to learn Ruby 15-441 Spring 2010, Recitation 5 Your Awesome TAs 2/12/10 15-441 Ruby Recitation 1

Why do we want a scripting language? Why not Assembly, C, C++, Java.. Much easier to program in Shorten the edit-develop-compile cycle Re-use existing components E.g. TCP server, retrieve web pages Easy short-cuts for common operations Text-processing, Strings, Regexp Fewer low-level hassles Types, memory management etc 2/12/10 15-441 Ruby Recitation 2

Some examples Shell-script Sed/Awk Perl Python Tcl/Tk Smalltalk.. 2/12/10 15-441 Ruby Recitation 3

Some downsides.. Most commonly cited: Performance Not good for.. Compute-intensive operations Creating data structures, algorithms Less true as hardware makes up.. Common problem: unpredictable.. Interpreted, not compiled Don t require types/initialization Another common problem: mysterious.. From the manpage: Perl actually stands for Pathologically Eclectic Rubbish Lister, but don't tell anyone I said that. 2/12/10 15-441 Ruby Recitation 4

Ruby.. Some background Often called multi-paradigm Procedural + OOP + Functional features But a high-level scripting language! Philosophy: Principle of Least Surprise What you expect is most likely what you get Features Truly object-oriented Support for Perl-like regular expressions Syntax a lot like Python/Perl Trivia: The language was created by Yukihiro "Matz" Matsumoto, 1995 2/12/10 15-441 Ruby Recitation 5

Okay Lets get started File: Helloworld.rb #! /usr/bin/ruby #<-- # please have useful comments # unlike the one here! def sayhelloworld(name) #<-- puts "Hello world #{name} " #<-- #<-- sayhelloworld( kaushik") #<-- 2/12/10 15-441 Ruby Recitation 6

Basic syntax rules Comments start with a # character, go to EOL Each expression is delimited by ; or newlines Using newlines is the Ruby way Local variables, method parameters, and method names should all start with a lowercase letter or _ Global variables are prefixed with a $ sign Class names, module names, and constants should start with an uppercase letter Class instance variables begin with an @ sign 2/12/10 15-441 Ruby Recitation 7

Control structures The usual suspects if, while, for, until Iterators: each if example if (score > 10) puts "You have cleared the checkpoint elsif (score > 5) # this is cool! puts " You have passed else puts "You have failed :-( 2/12/10 15-441 Ruby Recitation 8

Control structures.. while example team = 1 while (team <= maxteam) result = grade(team) team = team + 1 Shortcuts: puts "Passed!" if (score >= 10) score = score+1 while (score <= 100) 2/12/10 15-441 Ruby Recitation 9

Arrays array1 = Array.new array1[0] = 1 array1[1] = 2 index = 0 #traditional way while (index < array1.size) puts array1[index].to_s index = index + 1 array2 = [3, 4, 5, 6] array2.each { x puts x} #Ruby way Useful functions: reverse, sort 2/12/10 15-441 Ruby Recitation 10

Hashes Most amazing feature of scripting languages Along with regular expressions hash1 = Hash.new hash1["champions"] = "steelers hash1["runnersup"] = "seahawks hash1.each do key,value puts "#{key} are #{value} hash1.delete("runnersup") e.g. where you might use this nick2ip["nick"] = ipaddr_connect 2/12/10 15-441 Ruby Recitation 11

Strings and regular expressions Strings s = This is a new string earl = Earl s = My name is #{earl} answer = 42 s = The answer name is + answer.to_s Many useful functions: to_i,upcase,downcase,reverse Note: need explicit to_i (unlike perl) Regular expression matching if string =~ / Hello\sWorld/ puts "the string is Hello World Commonly used regular expressions: \s, \w, \d,., *, + 2/12/10 15-441 Ruby Recitation 12

Strings and regular expressions.. Substitution: language.sub(/perl/,'ruby') #first language.gsub(/perl/,'ruby') #all Interested in not only matching but also values? s="12:50am if s=~/(\d+):(\d+)(\w+)/ puts "Hour:#$1, Min:#$2 #$3 split example helloworld= Hello World (hello,world) = helloworld.split numbers= 1,2,3,4,5 splitarray = numbers.split(, ) 2/12/10 15-441 Ruby Recitation 13

Code blocks and yield Code blocks defined by {} or do- yield example: def method_yields yield method_yields { puts "Came here"} Fancier example: def fibupto(max) i1, i2 = 1, 1 # parallel assignment while i1 <= max yield i1 i1, i2 = i2, i1+i2 fibupto(1000) { f print f, " " } each and iterators defined this way 2/12/10 15-441 Ruby Recitation 14

Basic file I/O Common printing tasks: printf, print, puts Getting user input: gets Reading a file 1. afile = File.open("testfile, r ) # process the file line by line afile.each_line do line line_new = line.chomp puts line_new 2. IO.foreach( testfile ) { f puts f} Getting rid of pesky EOL: chomp, chomp!, chop, chop! Alternative reading whole file into array arr = IO.readlines("testfile ) 2/12/10 15-441 Ruby Recitation 15

Basic file I/O Writing to a file wfile = File.open("debuglog",'w') wfile.puts "debug message\n" wfile.close 2/12/10 15-441 Ruby Recitation 16

Classes class IRC #class name starts in capital attr_reader :server,:port #shortcut for access outside attr_writer :nick #shortcut for writing @nick outside def initialize(server, port, nick, channel) #constructor @server = server #instance variables start with @ @port = port @nick = nick @channel = channel def connect #another method #instance variables don t need declaration @server_connection = TCPSocket.open(@server,@port) def s(s) @server_connection.s(s) ircc = IRC.new($SERVER,$PORT,, ) #create an object of type IRC ircc.nick = kaushik #nick is writeable 2/12/10 15-441 Ruby Recitation 17

Topics Useful for Project Testing Controlling processes Network connections Handling exceptions Ruby unit testing 2/12/10 15-441 Ruby Recitation 18

Controlling processes System calls: system("tar xzf test.tgz") result = `date` IO.popen("ls -la") { f f.each_line { filename puts filename} } Forking a child if ((pid=fork) == nil) #in child exec( sort testfile > output.txt ) else #in parent puts the child pid is #{pid} cpid = Process.wait puts child #{cpid} terminated 2/12/10 15-441 Ruby Recitation 19

Controlling processes: Timeout Want to kill a child with a timeout Frequently needed with network tests if ( (cpid = fork) == nil) while (1) # this is the child puts "first experiment" else before = Time.now flag = 1 while (flag == 1) if ( (Time.now - before) > timeout) Process.kill("HUP",cpid) flag = 0 sleep(sleepinterval) 2/12/10 15-441 Ruby Recitation 20

Controlling processes: Timeout Want to kill a child with a timeout Frequently needed with network tests require timeout if ( (cpid = fork) == nil) # this is the child begin status = Timeout.timeout(5) do while (1) puts child experiment" rescue Timeout::Error puts Child terminated with timeout else puts Time.now Process.wait puts Time.now 2/12/10 15-441 Ruby Recitation 21

Network Connections Simple tcp client require socket #use this module t=tcpsocket.new('localhost,'ftp') t.gets t.close Simple tcp-echo server require socket port = 34657 server = TCPServer.new( localhost,port) while (session = server.accept) input= #{session.gets} session.print #{input}\r\n session.close Slightly advanced server: ext GServer 2/12/10 15-441 Ruby Recitation 22

Handling Exceptions a.k.a Having to code in the real world f = File.open("testfile") begin #.. process rescue => err # local var to get exception #.. handle error puts Error #{err} ensure # executed always, put cleanup here f.close unless f.nil? ensure: optional, goes after rescue retry: Use with caution infinite loop! 2/12/10 15-441 Ruby Recitation 23

Ruby unit testing require socket require test/unit Class TestIRC < Test::Unit::TestCase def setup # runs setup before every test @irc = IRC.new( $SERVER, $PORT,, ) @irc.connect() def teardown # runs teardown after every test @irc.disconnect() def test_user_nick # can define many such functions @irc.s_user('please give me :The MOTD') assert(@irc.test_silence(1)) @irc.s_nick('gnychis') ## lookup types of asserts under docs assert(@irc.get_motd()) 2/12/10 15-441 Ruby Recitation 24

Useful resources http://www.ruby-doc.org/docs/programmingruby/ http://www.ruby-lang.org/en/documentation/quickstart/ http://www.zenspider.com/languages/ruby/quickref.html http://sitekreator.com/satishtalim/introduction.html ri Class, ri Class.method irb: do quick test of code snippets And of course google 2/12/10 15-441 Ruby Recitation 25

Parting thoughts.. Why scripting: programmer efficiency Treat them as programming languages It may be quicker to write a longer but cleaner script than an arcane one-liner Avoid getting stuck with religious battles Perl vs Ruby vs Python Bottom-line Be efficient Write good code 2/12/10 15-441 Ruby Recitation 26