Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common...
Transcript of Learning New Languages & Introduction to Ruby · Consistency—Ruby coding should follow common...
![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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/39.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/40.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/41.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/42.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/43.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/44.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/45.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/47.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/48.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/49.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/50.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/51.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/52.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/53.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/54.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/55.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/56.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/57.jpg)
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](https://reader034.fdocuments.in/reader034/viewer/2022042309/5ed5c018b81c373d543298f8/html5/thumbnails/58.jpg)
Comic of the Day...