Ruby Programming Language - Introduction
-
Upload
kwangshin-oh -
Category
Software
-
view
364 -
download
4
Transcript of Ruby Programming Language - Introduction
Ruby
Programming Lan-guage
Introduction28 Jun 2015
Kwangshin Oh
http://kwangshin.pe.kr
Why New Programming Lan-guage?“Learning a new programming language will show you
new ways of thinking, and new approaches to problem solving
that will help you be a better programmer in any language.”
- Pragmatic Bookshelf
“Ultimately, programming is about understanding, and understanding is about ideas. So, exposure to new ideas is essential to a deeper understanding of what
programming is all about.”
- Foreword By Joe Armstrong, creator of Erlang
Agenda Ruby is… History & Why?
With a Spoonful of Sugar So, Ruby is… Ruby: Interpreted Language
Ruby: Interpreted Language – Example Compare: Compiled Language - Java
Ruby: Object-Oriented Language Ruby: Pure Object-Oriented Language
Ruby: Dynamically Typed Language What is Duck Typing? Ruby: Duck Typing Code Example
Download & Installation Ruby Code Reference
Ruby is…Programming Language!
DynamicOpen Source
Focus on…SimplicityProductivity
Has an elegant syntaxNatural to readEasy to write
Image Source: Ruby Programming Language
History & Why?Ruby’s Creator
Yukihiro “Matz” Matsumoto
Created in about 1993Motivation
The primary motivation was to amuse himself!Just a hobby at the beginningNew object-oriented language that combines
characteristics from Lisp, Smalltalk and PerlTo enhance programmers’ productivity
“Ruby is simple in appearance, but is very complex inside, just like our human body.”- Matz, speaking on the Ruby-Talk mailing list, May 12th, 2000.
Image Source: Ruby Everywhere
With a Spoonful of SugarRuby with more syntactic sugar
Makes code easier to read and write
Focus on the efficiency of the pro-
grammers
“A spoonful of sugar makes the medicine go down.”
Mary Poppins
Image Source: Disney Wiki – A Spoonful of Sugar
So, Ruby is… Interpreted Language
Ruby code is executed by an interpreter
Object-Oriented LanguageEncapsulationInheritancePolymorphism
Dynamically Typed LanguageTypes are bound at execution time
Image Source: Skillcrush - Ruby
Interactive
Console
Ruby File (*.rb)
Ruby: Interpreted Language
Image Source: http://www.iconsdb.com, http://dryicons.com and http://findicons.com
Ruby Interpreter
Interactive
Console
Ruby File (Ha.rb)
Ruby: Interpreted Language - Example
D:\Ruby22\bin>type Ha.rb3.Times do print “Ha “endD:\Ruby22\bin>ruby Ha.rbHa Ha HaD:\Ruby22\bin>
D:\Ruby22\bin>ruby3.Times do print “Ha “end^DHa Ha HaD:\Ruby22\bin>
Compare: Compiled Language - Java Java Source
File (*.java)
JavaCompiler
Java Class File (*.class)
JRE(Java Runtime Environment)
Ruby: Object-Oriented LanguageEncapsulation
[State & Behavior] : Packaged together
Inheritance[Class Tree] : Object types are organized
Polymorphism[Many Forms]
: Objects can be shown
Image Source: What Is an Object?
Image Source: What Is Inheritance?
Image Source: Object-Oriented Concepts
Ruby: Pure Object-Oriented Language
Ruby Programming LanguageEverything is an object!
Compare: Java Programming LanguagePrimitive types (int, char, ……) are not objects!
D:\Ruby22\bin>irbirb(main):001:0> 4=> 4irb(main):002:0> 4.class=> Fixnumirb(main):003:0> 4 + 4=> 8irb(main):004:0> 4.methods=> [:to_s, :inspect, :-
@, :+, ∙∙∙∙∙∙, :__id__]irb(main):005:0>
Ruby: Dynamically Typed Lan-guage
Types are bound at execution time
FlexibilityExecution Safety
Types are bound at compile time
Flexibility Execution Safety
Dynamically Typed
Statically Typed
Java
What is Duck Typing?Quacks like a duck
Swims like a duck
Walks like a duck
Call it a duck
“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.” - James Whitcomb
RileyImage Source: https://www.iconfinder.com and http://www.vectors4all.net/
Ruby: Duck Typing Code Exam-ple
D:\Ruby22\bin>irbirb(main):001:0> i = 0=> 0irb(main):002:0> a = ['100', 100.0]=> ["100", 100.0]irb(main):003:0> while i < 2irb(main):004:1> puts a[i].to_iirb(main):005:1> i = i + 1irb(main):006:1> end100100=> nilirb(main):007:0>
We consider this to_i method as quack!
Duck typing doesn’t carewhat the underlying type might
be (whether string or float).
string
float
Download & Installation Current Stable Version
Ruby 2.2.2 (As of 28 Jun 2015)
WindowsRubyInstaller - http://rubyinstaller.org/downloads/
OS XHomebrew - http://brew.sh/
Debian GNU/Linux and Ubuntuapt – command : $ sudo apt-get install ruby-full
CentOS, Fedora, and RHELyum – command :$ sudo yum install ruby
Ruby: Sample Code
D:\Ruby22\bin>rubyproperties = ['object oriented', 'duck typed', 'productive', 'fun']properties.each {|property| puts "Ruby is #{property}."}^DRuby is object oriented.Ruby is duck typed.Ruby is productive.Ruby is fun.D:\Ruby22\bin>
Ruby Code: Hello World!
D:\Ruby22\bin>irbirb(main):001:0> puts 'Hello World!'Hello World!=> nilirb(main):002:0> language = 'Ruby'=> "Ruby"irb(main):003:0> puts "Hello, #{language}"Hello, Ruby=> nilirb(main):004:0> language = 'my Ruby'=> "my Ruby"irb(main):005:0> puts "Hello, #{language}"Hello, my Ruby=> nilirb(main):006:0>
Ruby’s Interactive Console
<One Quote String>Interpreted Literally
<Two Quotes String>
String Evaluation
Ruby Code: Comparison Opera-tors
D:\Ruby22\bin>irbirb(main):001:0> x = 4=> 4irb(main):002:0> x < 5=> trueirb(main):003:0> x > 4=> falseirb(main):004:0> false.class=> FalseClassirb(main):005:0> true.class=> TrueClassirb(main):006:0> puts 'X value is equal to 4.' if x == 4X value is equal to 4.=> nilirb(main):007:0>
Everything is an object in Ruby!
Ruby Code: Loops – until & while
D:\Ruby22\bin>irbirb(main):001:0> x = 5=> 5irb(main):002:0> x = x - 1 until x == 1=> nilirb(main):003:0> x=> 1irb(main):004:0> while x < 5irb(main):005:1> x = x + 1irb(main):006:1> puts xirb(main):007:1> end2345=> nil
Ruby Code: Expressions - true & false
D:\Ruby22\bin>irbirb(main):001:0> puts '1 is true in Ruby' if 11 is true in Ruby=> nilirb(main):002:0> puts 'String is true in Ruby' if 'Kwangshin Oh'(irb):2: warning: string literal in conditionString is true in Ruby=> nilirb(main):003:0> puts '0 is also true in Ruby' if 00 is also true in Ruby=> nilirb(main):004:0> puts 'The false is false in Ruby' if false=> nilirb(main):005:0> puts 'The nil is false in Ruby' if nil=> nilirb(main):006:0>
Everything( but nil and false ) evaluate to true!
Reference Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Lan-
guage by Bruce A. Tate https://pragprog.com/book/btlang/seven-languages-in-seven-weeks
Ruby Programming Language https://www.ruby-lang.org/
Ruby-Talk mailing list, May 12th, 2000 http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/2773
Disney Wiki – A Spoonful of Sugar http://disney.wikia.com/wiki/A_Spoonful_of_Sugar
Matz にっき (Matz Diary) http://www.rubyist.net/~matz/
The Java™ Tutorials - Object-Oriented Programming Concepts https://docs.oracle.com/javase/tutorial/java/concepts/index.html
Duck typing https://en.wikipedia.org/wiki/Duck_typing