scripting in ruby by amber bennett “ruby is simple in appearance, but is very complex inside, just...
TRANSCRIPT
![Page 1: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/1.jpg)
SCRIPTING IN RUBY
By Amber Bennett
“Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto
![Page 2: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/2.jpg)
HISTORY
Created by Yukihiro Matsumoto Initial release in 1995 Matsumoto desired a scripting language with object-
oriented support
Photo Credit: "Yukihiro Matsumoto EuRuKo 2011" by Mathias Meyer - http://www.flickr.com/photos/ipom/5862768190/. Licensed under Creative Commons Attribution-Share Alike 2.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:Yukihiro_Matsumoto_EuRuKo_2011.jpg#mediaviewer/File:Yukihiro_Matsumoto_EuRuKo_2011.jpg
![Page 3: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/3.jpg)
NAMES, BINDINGS, AND SCOPES
Variable names can start with numbers, letters, and any combination of the two They must start with a letter or an underscore
Variable names are descriptive Case sensitivity:
Class names must be capitalized Variable names are begun with lowercase letters
![Page 4: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/4.jpg)
Class variables Exist in the scope of a class
Instance variables Unique to a given instance of a class
Constants Do not change throughout execution of a program Can be reassigned, but interpreter will give a warning
![Page 5: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/5.jpg)
![Page 6: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/6.jpg)
DATA TYPES
Dynamically typed Class variables start with @@ Instance variables start with @ Constants begin with a capital letter String type Number type Symbols Arrays and Hashes Abstract Data Types
Provided through classes in Ruby
![Page 7: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/7.jpg)
STRINGS: THE DIFFERENCE BETWEEN SINGLE AND DOUBLE QUOTES
Double Quotes Single Quotes
The code sample: “The time is #{Time.now}”
Prints out:The time is 2014-05-23 07:26:12 +0000
The code sample:‘The time is #{Time.now};
Prints out:The time is \#{Time.now}
![Page 8: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/8.jpg)
SYMBOLS
1.# p039xysymbol.rb
2.know_ruby = :yes
3.if know_ruby == :yes
4. puts 'You are a Rubyist'
5.else
6. puts 'Start learning Ruby'
7.end
Source: http://rubylearning.com/satishtalim/ruby_symbols.html
![Page 9: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/9.jpg)
ARRAYS AND HASHES
Common among other languages 0 indexed Arrays are defined by [] Hashes are defined by curly braces in the format:
hash_name = { :key1 => “value1”, :key2 => “value2” }
![Page 10: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/10.jpg)
EXPRESSIONS AND ASSIGNMENT STATEMENTS Operators
[] []= Assignment * / % + ** Arithmetic <= => < > Comparison .. … Range & ^ | AND, XOR, regular OR (bitwise) || && not or and Logical operators
Ternary operator: expression ? value_if_true : value_if_false
![Page 11: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/11.jpg)
STATEMENT-LEVEL CONTROL STRUCTURES If-statements
Then, else, end
Case Expressions Blocks
1.if sum == 0 then
2. if count == 0 then
3. result = 0
4. end
5.else
6. result = 1
7.end
![Page 12: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/12.jpg)
CASE EXPRESSIONS
![Page 13: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/13.jpg)
OBJECT-ORIENTED SUPPORT
Matsumoto created Ruby with object-oriented support in mind
A lot of functionality when dealing with files Matsumoto “wanted a scripting language that was more
powerful than Perl, and more object-oriented than Python.”
Video!
![Page 14: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/14.jpg)
CONCURRENCY AND EXCEPTION AND EVENT HANDLING Logical concurrency
Global Interpreter Lock (GIL)
Exceptions can be caught using “rescue”
![Page 15: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/15.jpg)
![Page 16: SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” --Yukihiro Matsumoto](https://reader036.vdocument.in/reader036/viewer/2022082817/56649e535503460f94b4914e/html5/thumbnails/16.jpg)
SOURCES
Code samples taken from (unless otherwise specified): http://sandbox.mc.edu/~bennet/ruby/code/index.html