scripting in ruby by amber bennett “ruby is simple in appearance, but is very complex inside, just...

16
SCRIPTING IN RUBY By Amber Bennett “Ruby is simple in appearance, but is very complex inside, just like our human body.” -- Yukihiro Matsumoto

Upload: noah-lang

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

SOURCES

Code samples taken from (unless otherwise specified): http://sandbox.mc.edu/~bennet/ruby/code/index.html