Ruby and other languages….

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
 2002 Prentice Hall. All rights reserved. 1 Intro: Java/Python Differences JavaPython Compiled: javac MyClass.java java MyClass Interpreted: python MyProgram.py.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Overview of C++ Chapter 2 in both books programs from books keycode for lab: get Program 1 from web test files.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
String Escape Sequences
Objectives You should be able to describe: Data Types
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Computer Science 101 Introduction to Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
COMP 171: Data Types John Barr. Review - What is Computer Science? Problem Solving  Recognizing Patterns  If you can find a pattern in the way you solve.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Python Let’s get started!.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
C++ First Steps.
Definition of the Programming Language CPRL
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
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)
Python Let’s get started!.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
ECE Application Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Variables, Expressions, and IO
JavaScript.
Introduction to C++ Programming
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Python Primer 1: Types and Operators
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Topics Designing a Program Input, Processing, and Output
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Tutorial 10: Programming with javascript
Building Java Programs
In this class, we will cover:
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Building Java Programs
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Ruby and other languages…

Valuable Reference The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)

Ruby Program Structure Basic unit is expression Primary expressions: true, false, nil, self, number and string literals, variable references (all represent values) Expression types: arithmetic, boolean Expressions produce a value Statements generally do something, may contain expressions (distinction blurry sometimes) http://stackoverflow.com/questions/4728073/what- is-the-difference-between-an-expression-and-a- statement-in-python

Get started option #1 - interactive open IRB type Ruby command, press return. Example: puts “say Hi” say Hi nil nil is return… ruby has expressions, not statements

Getting started option #2 – command line Open text editor Write ruby commands, e.g.: puts “say Hi” save file as demo1.rb at command line: ruby demo1.rb you must be in the same folder as demo1.rb On school computers, type Z: and press return to get to the z: drive Use cd to navigate (you’ve done this with git bash)

Ruby Program Structure Code can be organized using: Blocks Methods Classes Modules

Program Execution Ruby is a scripting language No special main method In general, script starts executing with line 1, continues until all lines executed Methods/classes come into existence when they are read in the file. Method calls must be placed after the method definitions

Ruby Expression Structure Whitespace: mostly ignored Expression separators: newline use caution if statement doesn’t fit on one line insert newline after operator, period or comma OR escape the newline * * think: how does interpreter recognize tokens/statement?

Block Structure 10.times { puts “hello” } x = 5 unless x == 10 print x end Blocks can be nested. Indent for clarity. block surrounded by { } often call this the “body” never delimit with { } block delimited by “end” Compare to Java/C++

Ruby Comments # This is a comment OR =begin This is a longer comment. =begin/=end must be at the start of a line =end

Variables length: no limit (afaik) valid characters: letters, numbers, _ can’t start with number $ used as first character of global var @/@@ used to identify instance and class variables ? (convention) end method name with ? if returns boolean ! (convention) end method name with ! if dangerous = used to make assignments (covered with classes) no other punctuation support for Unicode first letter (enforced by ruby): Constants, classes and modules begin with A-Z case sensitive Compare to Java/C++/Fortran

Ruby Data Types: numbers Numeric Integer – allows base 8, 16, 2 (binary) Fixnum: fit in 31 bits Bignum: arbitrary size Float – includes scientific notation Complex BigDecimal: use decimal rather than binary rep Rational C++ has unsigned ints, Java does not… concept doesn’t apply to Ruby – why? COBOL was for business… inherent big decimal. Java/C# provide. C++ does not. Adv: accuracy. Disadv: waste space

A few number-related details div = integer division, e.g., 7.div 3 fdiv = floating point division, e.g., 7.fdiv 3 quo = rational division -7/3 = -3 in Ruby, -2 in Java/C++ Float::MAX Infinity Numbers are immutable (as you’d expect)

Ruby Data Types: strings String literals – single quote ‘A ruby string’ ‘Didn\’t you have fun?’ Only escape \’ or \\ newlines are embedded if multi-line String literals – double quote normal escape sequences (\t, \n etc) string interpolation w=5 h=4 puts "The area is #{w*h}" Other languages with interpolation?

More strings Strings are mutable in Ruby + is concatenation (often prefer interpolation) age = 32 puts "I am " + age.to_s << is append s = "Hello" s << " World" puts s Extract characters puts s[0, 5] * repeats text puts "hey " * 5 Java converts right-hand to string, Ruby doesn’t

Characters Changed from Ruby 1.8 to Ruby 1.9 Characters are now strings of length 1 Python also has strings of length 1, not primitive chars

Ruby Methods No ( ) needed for function invocation Try it: "hello".center 20 "hello".delete "lo" note: if use (), don’t put space after fn name! f(3+2)+1 != f (3+2)+1 What’s good practice? Here are some thoughts: http://stackoverflow.com/questions/340624/do-you-leave-parentheses-in-or-out-in-ruby

String Access Can use [] with, e.g., s = "cats rule" [ix] # puts s[0] [ix,len] # puts s[0,4] [ix..ix] # puts s[0..3] [-ix] #puts s[-4, 4] puts s.length if index too large, just returns nil Compare to C/C++

String Access: Quick Exercise Try: s = "Sunday, Monday, Tuesday, Wednesday, Thursday, Friday" Find different ways to extract Sunday, Monday and Friday using the index options shown above Use different ways to modify the string (e.g., convert the string to: Monday, day, Tuesday, Wednesday, Thursday, Saturday) Play with RubyBasics.rb Nothing to submit; no right answers – just play!

Get started Do Ruby Intro homework

Topic Summary Ruby Basics Language concepts program structure Effect of syntax on compilation Binding overview static vs dynamic explicit vs implicit types Scripting language Keywords vs Reserved words Ruby Basics program structure program execution block structure methods comments variables strings interpolation numbers

Extra Details May use BEGIN/END (not common to do) BEGIN { # global init code ] END { #global shutdown code] if multiple BEGINS, interpreter executes in order read