learning new languages & introduction to ruby · consistency—ruby coding should follow common...

58
Learning New Languages & Introduction to Ruby Slides adapted from Prof. Heimann CMU-P

Upload: others

Post on 29-May-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Learning New Languages & Introduction to Ruby

Slides adapted from Prof. Heimann CMU-P

Page 2: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Philosophy of Ruby

“For me, the purpose of life is, at least partly, to have joy. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby is designed to make programmers happy.” !

— Yukihiro Matsumoto

Page 3: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Three Principles

1. Conciseness—Writing code in Ruby should involve the minimum amount of commands necessary. Code should be terse but also understandable.

2. Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous.

3. Flexibility—There is no one right way. You should be able to pick the best approach for your needs and be able to even modify the base classes if necessary.

These three together lead to an important concept in Ruby — the principle of least surprise.

Page 4: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Comic of the Day...

Page 5: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

The Ruby Way

Page 6: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

The Ruby Way

Page 7: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

The Ruby Way

Page 8: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

The Ruby Way

Page 9: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Data Types and Structures

Page 10: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Learn about basic data types & structures

• Examples

• Strings

• Numbers

• Arrays

• Hashes

Page 11: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Everything is an object

Looking at Strings, we see:

Page 12: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Revising the String class

Page 13: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Determining what methods are available

Page 14: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Destructive and Predicate methods

Page 15: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Object public methods

Page 16: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Looking at numbers

Page 17: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Numeric public methods

Page 18: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Numeric public methods

Page 19: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Tax example

Page 20: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Add some formatting

Page 21: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Arrays

Page 22: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Arrays

Page 23: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Arrays

Page 24: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Hashes

Page 25: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Control Structures

Page 26: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 27: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 28: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 29: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 30: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 31: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 32: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 33: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 34: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 35: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 36: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 37: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 38: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 39: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 40: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Loops

Page 41: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 42: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 43: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 44: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 45: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 46: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 47: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 48: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 49: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 50: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 51: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

- or -

Page 52: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 53: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 54: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 55: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 56: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

Page 57: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Blocks

- or -

Page 58: Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common conventions that make coding intuitive and unambiguous. 3. Flexibility—There is no

Comic of the Day...