17-Jun-15 Assorted Ruby Details. The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Perl Arrays and Lists Software Tools. Slide 2 Lists l A list is an ordered collection of scalar data. l A list begins and ends with parentheses, with.
Input from STDIN STDIN, standard input, comes from the keyboard. STDIN can also be used with file re-direction from the command line. For instance, if.
Perl Arrays and Lists Learning Objectives: 1. To understand the format and the declaration of Arrays & Lists in Perl 2. To distinguish the difference between.
10-Jun-15 Ruby (Bet you can’t do this in Java!). “Hello World” in Ruby puts "Hello World!"
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Scripting Languages Chapter 6 I/O Basics. Input from STDIN We’ve been doing so with $line = chomp($line); Same as chomp($line= ); line input op gives.
Perl I/O Software Tools. Lecture 15 / Slide 2 Input from STDIN Reading from STDIN is easy, and we have done it many times. $a = ; In a scalar context,
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Guide To UNIX Using Linux Third Edition
Unix Filters Text processing utilities. Filters Filter commands – Unix commands that serve dual purposes: –standalone –used with other commands and pipes.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
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.
Homework Reading Programming Assignments
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
CMPE13 Cyrus Bazeghi Chapter 18 I/O in C. CMPE Standard C Library I/O commands are not included as part of the C language. Instead, they are part.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
 Pearson Education, Inc. All rights reserved Formatted Output.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
C Tokens Identifiers Keywords Constants Operators Special symbols.
23-Oct-15 Ruby! (Bet you can ’ t do this in Java!)
Chapter 18 I/O in C.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File IO and command line input CSE 2451 Rong Shi.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Time to talk about your class projects!. Shell Scripting Awk (lecture 2)
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Standard Types and Regular Expressions CS 480/680 – Comparative Languages.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
21-Feb-16 Assorted Ruby Features (Bet you can’t do this in Java!)
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CMSC330 More Ruby. Last lecture Scripting languages Ruby language –Implicit variable declarations –Many control statements –Classes & objects –Strings.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
C Formatted Input/Output
Lesson 5-Exploring Utilities
Input from STDIN STDIN, standard input, comes from the keyboard.
CST8177 sed The Stream Editor.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Chapter 18 I/O in C.
The Ruby Programming Language
File Input/Output.
Programming in C Input / Output.
“The fastest I/O is no I/O.” Nils-Peter Nelson, Bell Labs
Lecture 13 Input/Output Files.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to C++ Programming
(Bet you can’t do this in Java!)
File Input and Output.
Perl I/O Learning Objectives:
Fundamental of Programming (C)
Fundamentals of Python: First Programs
Module 12 Input and Output
Presentation transcript:

17-Jun-15 Assorted Ruby Details

The command line irb starts an interactive Ruby interpreter ruby starts Ruby, with input from the command line End with an end-of-file character ( ^D or F6 ) Not actually very useful ruby filename.rb executes the given file ruby -e quotedString executes the quoted string Example: ruby -e 'puts "The time is #{Time.now}" ' ruby -v tells you Ruby’s version number On Unix, if the first line of a Ruby file is #!/usr/bin/ruby (or wherever ruby is located), the file can be executed by just entering the file name

Adding and removing methods def adds a method; undef removes a method The only parameter to undef is the method name To add an instance method, first “open” the class Example: class String; def nchars; length; end; end There are several ways to add a class method to a class def Person.species; 'human'; end Within the Person class, you can use def self.species You can add a method to individual objects def oscar.mood; 'grouchy' ; end

Numbers Numbers may be written in decimal, hexadecimal, octal, or binary Decimal: Hex: 0xCAFEBABE or 0XCAFEBABE Octal: or 0o Binary: 0b or 0B etc. For readability, numbers may contain (but not begin or end with) underscores Examples: 3_405_691_582, 0b_111_101_101 Integers may be indexed to retrieve their bits Example: 5.step(0, -1) { |i| print 6[i] } 

printf and friends printf format_string, value, …, value Formats are % length code for most things, % length.fractional_digits code for floats %d  decimal, %o  octal, %x  hex, %b  binary, %f  float, %s  string Negative lengths mean left justified Various other controls Example: printf "pi = %8.4f",  pi.=… The (equivalent) methods sprintf and format take the same parameters as printf, but return the resultant string rather than printing it

Some File < IO methods gets – get a line of text getc – get a character of text (as ASCII; use.chr ) ungetc – put back a character pos – the current character position in the input stream lineno – the number of times gets has been called pos= – move to the given position in the file rewind – move to the beginning of the file readlines – read the stream as an array of strings write( string ), print( string ), <<( string ) – write at the current position eof? – test if at the end of file closed? – test if the file has been closed

Some File methods rename( oldname, newname ) – rename a file read( filename ) – read the entire file as a single string readlines( filename ) – read the entire file as an array of strings open( filename ) – with no block, a synonym for File.new with a block, the file is passed to the block, and automatically closed when the block finishes exists?( filename ) – test if a file with that name exists writable?( filename ) – test if the file can be written directory?( filename ) – test if the file is a directory zero?( filename ) – test if the file is empty size( filename ) – returns the size of the file mtime( filenam e) – returns the modification time of the file

Streams The following constants refer to standard I/O streams: STDIN, STDOUT, STDERR The following variables are initially set to the corresponding constants: $stdin, $stdout, $stderr In addition, $defout (initially equal to $stdout ) is where output with no specified destination is sent

Some String methods ljust( length ), center( length ), rjust( length ) – left justify, center, or right justify the string by padding with spaces downcase, upcase, swap, capitalize – modify capitalization include?( s_or_c ) – tests whether the string includes the given string or character index( s_or_c [, offset ]) – returns the index after offset(or nil ) at which the gives string starts rindex( s_or_c [, limit ]) – returns the last index (before limi t ), or nil, at which the string starts

Some more String methods strip – remove leading and trailing spaces chop – remove the last character (also chop! is destructive) chomp – remove the last character if it is a newline (also chomp! ) tr( chars, replacement ) – replace the characters in chars with the corresponding characters in replacement; accepts ch1-ch2 notation

Some Array methods min, max – return the smallest or largest element uniq – return an array with no duplicate elements compact – return an array with no nil elements sort – return a sorted array & – perform an intersection (only elements in both) | – perform a union (elements in either) grep( regexp) – return elements matching the pattern push( element) – add the element to the end of the array pop – remove and return the last element shift – remove and return the first element

Chaining Nondestructive methods can usually be chained Example: x = gets.chomp.strip.downcase Many destructive methods return nil if they make no changes in the receiver, hence cannot be chained Example: x = gets.chomp!.strip!.downcase! will result in a runtime error

Iterators In Ruby, loops are considered low-level, to be used only when there is no appropriate iterator collection.each – step through every element n.times – do a block n times n.downto( limit ) – step from n down to and including limit n.upto( limit ) – step from n up to and including limit string.each_line – get each line from a string string.each_char – get each character (as an integer) from a string

More iterators collection.each_index – iterate over the indices of a collection collection.each_with_index – iterate over the values in a collection, along with their indices Example: lineup.each_with_index { |man, pos| print pos, man } hash.each_key – iterate over keys hash.each_value – iterate over values hash.each_pair – iterate over key-value pairs collection.select { | v | condition } – choose only items that meet the condition collection.map { | v | transformation } – create a new collection with the transformation applied to each item

The End