CS代写 Evaluate (each is true or false): 3 == 3

Evaluate (each is true or false): 3 == 3
3.equal? 3
“hello” == “hello” “hello”.equal? “hello”
Computer Science and Engineering  The Ohio State University

Copyright By PowCoder代写 加微信 powcoder

Objects and Dynamic Types
Computer Science and Engineering  College of Engineering  The Ohio State University

Primitive vs Reference Types
Computer Science and Engineering  The Ohio State University
Recall Java type dichotomy:
Primitive: int, float, double, boolean,… Reference: String, Set, NaturalNumber,…
A variable is a “slot” in memory
Primitive: the slot holds the value itself Reference: the slot holds a pointer to the value (an object)
width: 12 height: 15 color: “blue”

Object Value vs Reference Value
Computer Science and Engineering  The Ohio State University
Variable of reference type has both:
Reference value: value of the slot itself Object value: value of object it points to (corresponding to its mathematical value)
Variable of primitive type has just one Value of the slot itself, corresponding to its mathematical value
width: 12 height: 15 color: “blue”

Two Kinds of Equality
Computer Science and Engineering  The Ohio State University
Question: “Is x equal to y?”
A question about the mathematical value of the variables x and y
In Java, depending on the type of x and y we either need to:
Compare the values of the slots x == y // for primitive types
Compare the values of the objects x.equals(y) // for non-primitive types

Ruby: “Everything is an Object”
Computer Science and Engineering  The Ohio State University
In Ruby, every variable maps to an object Integers, floats, strings, sets, arrays, …
Benefit: A more consistent mental model References are everywhere
Every variable has both a reference value and an object value
Comparison of mathematical values is always comparison of object value
Ruby terminology: Reference value is called the “object id”
The 4- or 8-byte number stored in the slot
Unique identifier for corresponding object
msg = “shark”
msg.object_id #=> 1544150170

Everything is an Object
Computer Science and Engineering  The Ohio State University
width: 12 height: 15 color: “blue”
1544150170

Operational Detail: Immediates
Computer Science and Engineering  The Ohio State University
For small integers, the mathematical value is
encoded in the reference value!
LSB of reference value is 1
Remaining bits encode value, 2’s complement
x.object_id #=> 1 (0b00000001)
y.object_id #=> 13 (0b00001101)
Benefit: Performance
No change to model (everything is an object) Known as an “immediate” value
Other immediates: true, false, nil, symbols

Objects Have Methods
Computer Science and Engineering  The Ohio State University
Familiar “.” operator to invoke
(instance) methods
list = [6, 15, 3, -2] list.size #=> 4
Since numbers are objects, they have
methods too!
3.to_s #=> “3”
3.odd? #=> true
3.lcm 5 #=> 15
3.+ 5 #=> 8
3.class #=> Integer
3.methods #=> [:to_s, :inspect, :+, …]

Pitfall: Equality Operator
Computer Science and Engineering  The Ohio State University
Reference value is still useful sometimes “Do these variables refer to the same object?”
So we still need 2 methods:
x.equal? y
Ruby semantics are the opposite of Java! == is object value equality
.equal? is reference value equality Example
s1, s2 = “hi”, “hi”
s1 == s2 #=> true (obj values equal) s1.equal? s2 #=> false (ref vals differ)

Evaluate (each is true or false): 3 == 3
3.equal? 3
“hello” == “hello” “hello”.equal? “hello”
Computer Science and Engineering  The Ohio State University

Assignment (Just Like Java)
Computer Science and Engineering  The Ohio State University
Assignment copies the reference value Result: Both variables point to the same object (ie an “alias”)
Parameter passing works this way too

Assignment (Just Like Java)
Computer Science and Engineering  The Ohio State University
Assignment copies the reference value Result: Both variables point to the same object (ie an “alias”)
Parameter passing works this way too

Assignment (Just Like Java)
Computer Science and Engineering  The Ohio State University
Assignment copies the reference value Result: Both variables point to the same object (ie an “alias”)
Parameter passing works this way too

Aliasing Mutable Objects
Computer Science and Engineering  The Ohio State University
When aliases exist, a statement can
change a variable’s object value
without mentioning that variable
x = [3, 4]
y = x # x and y are aliases y[0] = 13 # changes x as well!
Question: What about numbers?
j = i # i and j are aliases
j = j + 1 # does this increment i too?

Immutability
Recall in Java strings are immutable
No method changes the value of a string
A method like concat returns a new instance
Benefit: Aliasing immutable objects is safe
Immutability is used in Ruby too
Numbers, true, false, nil, symbols
list = [3, 4]
list[0] = 13 # changes list’s object value
# list points to same object
n = n + 1 # changes n’s reference value
# n points to different object Pitfall: Unlike Java, strings in Ruby are
Computer Science and Engineering  The Ohio State University

Assignment Operators
Parallel assignment
x, y, z = y, 10, radius
+= -= *= /= %= **=
Pitfall: no ++ or — operators (use += 1)
Idiom: ||= for initializing potentially nil
Pitfall (minor):
Computer Science and Engineering  The Ohio State University
Arithmetic contraction
Logical contraction
x ||= y not quite equivalent to x = x || y Better to think of it as x || x = y
Usually amounts to the same thing

Declared vs Dynamic Types
Computer Science and Engineering  The Ohio State University
In Java, types are associated with both
Variables (“declared” / “static” type), and
Objects (“dynamic” / “run-time” type)
Queue line = new Queue1L();
Recall: Programming to the interface
Compiler uses declared type for checks line.inc(); //error no such method
line = new Set1L(); //error wrong type
boolean isEmpty (Set s) {…}
if isEmpty(line) … //error arg type

Statically Typed Language
Computer Science and Engineering  The Ohio State University
<1, 2, 8, 2>
width: 12 height: 15 color: “blue”

Dynamically Typed Language
Computer Science and Engineering  The Ohio State University
<1, 2, 8, 2>
width: 12 height: 15 color: “blue”

Dynamically Typed Language
Computer Science and Engineering  The Ohio State University
Equivalent definitions:
No static types
Dynamic types only
Variables do not have type, objects do

Object-oriented
Computer Science and Engineering  The Ohio State University
References are everywhere
Assignment copies reference value (alias) Primitives (immediates) are objects too == vs .equal? are flipped

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com