Presentation is loading. Please wait.

Presentation is loading. Please wait.

And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)

Similar presentations


Presentation on theme: "And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)"— Presentation transcript:

1 and other languages…

2  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)

3 How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references – type conversions and equality Expressions – Operators, overloading, booleans, short-circuiting, conditional expression – Referential transparency – Statements vs Expressions Control flow – conditionals – loops Functions Classes Exception handling Other features Threads Reflection Libraries Functional Language – other aspects, covered later QUICK EX: With a partner how do you learn a new programming language? What types of programs do you write?

4

5  A binding is an association, such as: bind type of variable bind operation to symbol (e.g., meaning of *) bind function to its definition  Binding time is the time at which a binding takes place.  Type binding may be static or dynamic explicit or implicit

6  Language design time -- bind operator symbols to operations : sum = sum + count  Language implementation time-- bind type to a representation : int => number of bits, etc.  Compile time -- bind a variable to a type: int count;  Link time – bind library subprogram to code: cout << x;  Load time -- bind a FORTRAN 77 variable to a memory cell (or a C static variable)  Runtime -- bind a nonstatic local variable to a memory cell

7  A binding is static if it first occurs before run time and remains unchanged throughout program execution.  A binding is dynamic if it first occurs during execution or can change during execution of the program  NOTE: doesn't consider paging etc. which is at the hardware level

8  Type not specified by declaration, not determined by name (JavaScript, PHP, Ruby)  Specified through an assignment statement list = [2, 4.33, 6, 8]; list = 17.3; Advantage: flexibility (generic program units) Disadvantages:  High cost (dynamic type checking requires run-time descriptors, normally interpreted… upcoming discussion)  Type error detection by the compiler is difficult How are generic program units done in C++? Java?

9  How would dynamic types be implemented? What data structure(s) would you use? How does this impact your code – consider efficiency, reliability.  Now think about challenges with + total = 3 + 5 message = “hello” + “ world” something = “count “ + 3 + 5 other = 3 + “count”

10 i = x; // desired, x is scalar i = y; // typed accidentally, y is array

11  An explicit declaration is a program statement used for declaring the types of variables: int count;  An implicit declaration is a default mechanism for specifying types of variables (the first appearance of the variable in the program)  Both create static bindings to types (i.e., type doesn’t change during execution of program)  FORTRAN, PL/I, BASIC, and Perl provide implicit declarations Advantage: writability Disadvantage: reliability  Perl: @ is array, % is hash, $ is scalar  Fortran: I-N integer, others single precision, can override

12  many words have special meaning (e.g. if, true, def, etc.)  Keyword: has special meaning in particular context, but can be used as variable name Algol, PL/I  Reserved: can’t be used as variable COBOL has ~400, Java has ~50 Advantage: may avoid confusion Disadvantage: may need to be aware of language parts you aren’t even using Compare to Java/C++

13

14  Basic unit is expression  Primary expressions: true, false, nil, self, number and string literals, variable references (all represent values)  Expression types: arithmetic, boolean  Code can be organized using: Blocks Methods Classes Modules Expression vs statement?

15  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

16  open IRB  puts “say Hi” say Hi  nil  nil is return… ruby has expressions, not statements

17  Open text editor  puts “say Hi”  save file as demo1.rb  at command line: ruby demo1.rb

18  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 * Compare to Java/C++/Python * think: how does interpreter recognize tokens/statement?

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

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

21  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

22  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

23  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)

24  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?

25  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

26  Changed from Ruby 1.8 to Ruby 1.9  Characters are now strings of length 1  Not covered multi-byte characters (need for unicode… 16-bit encoding, first 128 the same as ASCII) specify encodings (e.g., ASCII-8BIT, BINARY, US- ASCII, ASCII, ISO-8859-15, UTF-8) many other String methods, such as downcase, upcase, chop, delete, tr, etc. Does Java support unicode? Does C++? Python also has strings of length 1, not primitive chars

27  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 http://stackoverflow.com/questions/340624/ do-you-leave-parentheses-in-or-out-in-ruby Compare to Java/C++/Python

28  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++

29  Can use [] with: [ix] [ix,len] [ix..ix] [-ix] stringname.length, etc.  if index too large, just returns nil  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)  Nothing to submit; no right answers – just play! Compare to C/C++

30  Do Ruby Intro homework

31  Language concepts 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

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


Download ppt "And other languages….  The Ruby Programming Language, Flanagan & Matsumoto (creator of Ruby)"

Similar presentations


Ads by Google