Introduction to Ruby, part I

Similar documents
WSR Commands. WSR Commands: Mouse Grid: What can I say?: Will show a list of applicable commands

PHONETIC ALPHABET The use of standard phonetics for the pronunciation of letters in call signs and text aids accuracy and efficiency.

Installation Manual and User Guide

Clock Commands. Cisco IOS XR System Management Command Reference for the Cisco CRS Router, Release 4.2.x 1

Digital Messages Using Windows and Android Devices

DATA 301 Introduction to Data Analytics Data Representation. Dr. Ramon Lawrence University of British Columbia Okanagan

Clock Commands on the Cisco IOS XR Software

Clock Commands on the Cisco IOS XR Software

Clock Commands. Cisco IOS XR System Management Command Reference for the Cisco XR Series Router, Release 4.3.x OL

COMMUNICATION SYSTEMS RCT STUDY GUIDE Explain the importance of good communication.

Survey Reading the Notes. COSC 122 Computer Fluency. Information Representation. Survey Class Still Easy? Key Points

COSC 122 Computer Fluency. Information Representation. Dr. Ramon Lawrence University of British Columbia Okanagan

FOR EFFICIENT DICTATION

Advanced Python. Executive Summary, Session 1

Ruby: Introduction, Basics

15.1 Origins and Uses of Ruby

MathTalk. using. Dragon NaturallySpeaking. Pre-algebra, Algebra, Trig, Calculus Statistics, Graphing, etc. 1116

CS 5142 Scripting Languages

Cumberland County ARES/RACES A SHORT GUIDE TO EMERGENCY RADIO COMMUNICATIONS INTRODUCTION

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

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

Ruby: Introduction, Basics

FM 11-32: Team Speak

Lists, loops and decisions

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Python I. Some material adapted from Upenn cmpe391 slides and other sources

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Python. Executive Summary

Programming in Python

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Ruby Topic Maps. Introduction to Ruby. Benjamin Bock.

Your First Ruby Script

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

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

Ruby: Introduction, Basics

Introduction to Ruby. SWEN-250 Personal Software Engineering

Incident Command System Voice. Traffic Handling. User Guide WX4PBC Palm Beach County Skywarn Creative Commons

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Language Syntax version beta Proposed Syntax

ABC DNSSEC Key Ceremony Scripts

Programming Ruby The Pragmatic Programmers Guide

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

UC Getting Started. Overview. Getting Started page 1

CS111 Jeopardy: The Home Version

"Hello" " This " + "is String " + "concatenation"

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

Sequence types. str and bytes are sequence types Sequence types have several operations defined for them. Sequence Types. Python

Ruby: Useful Classes and Methods

Introduction to String Manipulation

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lecture 1. Basic Ruby 1 / 61

Strengthen Your Python Foundations

CS 11 python track: lecture 3. n Today: Useful coding idioms

G Programming Languages - Fall 2012

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

Positional, keyword and default arguments

Documentation for LISP in BASIC

CIS192 Python Programming

Computer Sciences 368 Scripting for CHTC Day 3: Collections Suggested reading: Learning Python

CIS192 Python Programming

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Sequences and iteration in Python

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

CMSC 330: Organization of Programming Languages

CS111: PROGRAMMING LANGUAGE II

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

Typed Racket: Racket with Static Types

5. VHDL - Introduction - 5. VHDL - Design flow - 5. VHDL - Entities and Architectures (1) - 5. VHDL - Entities and Architectures (2) -

MEIN 50010: Python Data Structures

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

CIS192 Python Programming

CS115 - Module 9 - filter, map, and friends

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Message Passing USE AND DISTRIBTION NOTICE. Housekeeping. Agenda. Message Passing Tabletop 1/31/2018

A Tour of Ruby.... for Java programmers

4. Write the output that would be printed from each of the following code fragments. (8pts) x = 9 y = x / 2 print('y ==', y) +1 4.

Iterators and blocks. Using iterators and blocks Iterate with each or use a for loop? Creating iterators

Introduction to Python

Artificial Intelligence A Primer for the Labs

EZ- ASCII: Language Reference Manual

OOP and Scripting in Python Advanced Features

Python A Technical Introduction. James Heliotis Rochester Institute of Technology December, 2009

FORM 2 (Please put your name and form # on the scantron!!!!)

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Structure and Flow. CS 3270 Chapter 5

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Variables, Constants, and Data Types

CS100: CPADS. Decisions. David Babcock / Don Hake Department of Physical Sciences York College of Pennsylvania

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Lists CS 1111 Introduction to Programming Fall 2018

Basic types and definitions. Chapter 3 of Thompson

Java Bytecode (binary file)

Python Evaluation Rules

Part I. Wei Tianwen. A Brief Introduction to Python. Part I. Wei Tianwen. Basics. Object Oriented Programming

COMP284 Scripting Languages Lecture 3: Perl (Part 2) Handouts

CS 115 Lecture 8. Selection: the if statement. Neil Moore

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Transcription:

Introduction to Ruby, part I April 20, 205 Output # evaluate expression and prints result. >> 4 + 5 => 9 >> 7 * 3 => 2 >> Hello, world! => "Hello, world!" # print text on standard output >> puts Hello, world! Hello, world! 2 Dynamically typed variables >> v = nil # Creates a new variable v on the stack holding # a reference to an instance of NilClass. >> v = 42 # Updates the v variable with a reference to an => 42 # a Fixnum object, with value 42. >> s = Hello # Creates a new variable s on the stack holding a => "Hello" # reference to a String object with value Hello. >> v + s # Invokes the +-method on the Hello String # object referenced by the s variable. This will # raise a TypeError as the String object does not # know how to add a Fixnum to a String. >> v + s # + is invoked on the Fixnum, resulting in a # TypeError as String cannot automaticly be

# converted to Fixnum. >> v.to_s # The to_s-method returns a printable string => "42" # representation of the object. In this case # "42". >> s + v.to_s # Returns the concatenated string "Hello42" => "Hello42" >> s.methods # Returns an array of the methods defined for the => ["upcase!",...] # object referenced by s. >> s, v = v, s # Multiple assignment: Swaps the values of v and s. => [42, "Hello"] 3 Lists >> l = [v, s] # Creates a list(array) containing Hello and 42. => ["Hello", 42] >> l << 3.0 # All kinds of types may be mixed in a list. => ["Hello", 42, 3.0] >> l.pop # Removes and returns the last element in list. => 3.0 # Can pop multiple values, i.e. l.pop(2) returns # a list of the last two elements. >> r =..4 # Creates a new instance of the Range class =>..4 # Useful when looping. x..y is inclusive the # last element, x...y is exclusive. >> l = r.to_a # Creates a new array from the range => [, 2, 3, 4] >> l = l + [5, 6] # Concatenation of lists => [, 2, 3, 4, 5, 6] # Loop over each element in the list l and summarise in sum. >> l.each do element?> the_sum += element # Above loop will crash as the_sum is unassigned, and therefore # considered nil. NameError is raised as nil has no + method. >> the_sum = 0 >> l.each do element 2

?> the_sum += element => [,2,3,4,5,6] # iteration returns the iterated object >> puts the_sum 2 # Alternate loop syntax >> l.each { element the_sum += element } 4 List access and slicing >> lst = [ a, b, c ] # Creates a new list from literal => ["a", "b", "c"] # representation >> lst = Array.new() # Creates a new list by calling => [] # constructor of the Array class # The index method [] takes one or two arguments. # The first is which index to retrieve, negative index means # count backwards from the end of the list. Second argument # is how many consecutive elements to returns as a new list. >> lst[] # Returns b as is the second position in list. => "b" >> lst[-] # Returns the last element of the list. => "c" >> lst[7] # Indexing outside of the list returns nil. # To make sure indexing outside the array results in errors, # one may use fetch instead. >> lst.fetch(7) IndexError: index 7 outside of array bounds: -4...4 from (irb):59:in fetch from (irb):59 from /usr/local/bin/irb:2:in <main> >> lst[,] # Returns a new list containing "b", [start, length]. => ["b"] >> lst[0,3] # Returns 3 elements from index 0, [start, length]. => ["a","b","c"] >> lst.dup # Returns a copy of the list. => ["a", "b", "c"] 3

>> lst.reverse # Returns a copy of the list reversed. ["c", "b", "a"] 5 Associative arrays (Hashes/Dictionaries/Maps?) # Create a new Hash, with unordered mappings. >> h = { one =>, two =>2, three =>3, four =>4 } => {"three"=>3, "two"=>2, "one"=>, "four"=>4} >> h["one"] # Update hash, key "three" refers the same value as # key "four". >> h["three"] = h["four"] => 4 # Print string representation of the contents of the Hash. >> puts h.inspect {"three"=>4, "two"=>2, "one"=>, "four"=>4} >> h.keys => ["three", "two", "one", "four"] >> h.has_key? "four" >> h.has_value? 4 # Is this one of the values? # Iterate over the key/value-pairs and print each. >> h.each { key, value puts "#{key} => #{value}" } three => 4 two => 2 one => four => 4 => {"three"=>4, "two"=>2, "one"=>, "four"=>4} Control Structures, iteration, ranges again # The Ruby if-statement begins with "if" and a conditional (no do) # and ends with "end" >> if nil >> print "nil is true" 4

>> if false >> print "false is true" # "nil" and "false" evaluates to false in Ruby, everything else # (including 0) eveluates to true >> if 0 >> print "0 is true" 0 is true # Only nil and false are false # Logical operators ("and" and "or") can be used to combine # conditionals >> if 0 and [] and {} and >> puts "0 and [] and {} and are true" (irb):5: warning: string literal in condition 0 and [] and {} and are true # If-statements can be combined using "elsif". The "else" clause # will be run if none of the conditionals above evaluated to # true. >> name = "Yukihiro" => "Yukihiro" >> if name == "Yukihiro" >> puts "Ruby" >> elsif name == "Guido" >> puts "Python" >> else?> puts "None of the above" Ruby # "or" and "and" will be evaluated as boolean true/false but # will also always return one of the objects in the expression >> names = { "Yukihiro" => "Ruby",?> "Guido" => "Python",?> } => {"Yukihiro"=>"Ruby", "Guido"=>"Python"} >> puts names[name] or "None of the above" Ruby 5

=> "None of the above" >> puts (names[name] or "None of the above") Ruby >> result = (false or Beatrice or false) => "Beatrice" >> if result >> puts result Beatrice >> result = ( Beatrice and [] and Isak ) => "Isak" >> if result >> puts result Isak # Negation of boolean values is done using "not". >> not false >> (not true) == false # The true and false values are represented by objects, and there # is only one true object and one false object >> (not true) === false >> (not true).object_id == false.object_id # The Ruby while-loop starts with "while" and a conditional and # ends with end >> i = 0 => 0 >> while i < 0 >> puts i >> i += 0. 6

. 8 9 # Several other ways of looping exists, e.g. the "each" iterator # in the Range class. >> lst =..0 =>..0 >> lst.each { i puts i} 2.. 8 9 0 =>..0 # Or a for loop on the range class. >> for i in..4 >> puts i 2 3 4 =>..4 # Below we see an example of operations that we could do with # lists. Using the "zip" method from the Array class we create a # new list containing lists of the elements from the original # lists that were found on the same index. >> cs = [ a, b, c ] => ["a", "b", "c"] >> ns = [,2,3] => [, 2, 3] >> zip = cs.zip(ns) => [["a", ], ["b", 2], ["c", 3]] # The list of lists is looped over using the "each" operator and # the elements of the lists inside the list are printed out. >> zip.each do ch, no?> puts "#{ch} #{no}" 7

a b 2 c 3 => [["a", ], ["b", 2], ["c", 3]] # The "collect" iterator will loop over the list collecting all # elements from the original list for which the conditional in the # code block evaluates to true. In this case, the code block # contains no conditional and all elements will be collected. >> chars = zip.collect { c, n c} => ["A", "b", "c"] >> nums = zip.collect { c, n n} => [, 2, 3] A first simple program # Let s say we have a file named "words.txt" with the following # words stored in it: # alpha # bravo # charlie # delta # echo # foxtrot # golf # hotel # india # juliet # kilo # lima # mike # november # oscar # papa # quebec # romeo # sierra # tango # uniform # victor # whiskey # xray # yankee # zulu 8

# The following code returns a list of words starting with a # (lowercase) vowel. >> matches = [] => [] >> vowels = "aoueiy".chars.to_a => ["a", "o", "u", "e", "i", "y"] >> f = open( words.txt ) => #<File:words.txt> >> f.each do line?> matches << line.strip if vowels.include? line.strip.chars.first => #<File:words.txt> >> matches => ["alpha", "echo", "india", "oscar", "uniform", "yankee"] 9