introduction to ruby Flashcards
History of ruby:
What was ruby influenced by?
•It was also influenced by Eiffel and Lisp, Perl with Smalltalk-like features.
What is Ruby?
Ruby is a dynamic, reflective, general-purpose object-oriented programming language.
Ruby and Java Comparison
•Ruby
sleep 100
•Java
public class sleep{
public static void main (String[] args) throws InterruptedException {
Thread.sleep (100*1000);
}
}
Who was the designer of Ruby?
Ruby was first designed and developed in the mid-1990s by Yukihiro “Matz” Matsumoto in Japan.”
The ruby language syntax is sensitive to the…
capitalization of identifiers, in all cases treating capitalized variables as constants.
•The sigils $ and @ do not indicate variable data type as in Perl, but rather….
function as scope resolution operators.
•One of ruby’s features is that it is thoroughly object-oriented with _____ , ______ , _______
inheritance mixins and metaclasses
What is Duck typing?
you don’t need a type in order to invoke an existing method on an object - if a method is defined on it, you can invoke it.
The name comes from the phrase “If it looks like a duck and quacks like a duck, it’s a duck”
What is Dynamic typing?
Dynamic typing just says that types are associated with run-time values, not with static variables and parameters.
Everything is an _________ (even statements) and everything is executed imperatively (even _________)
•Everything is an expression (even statements) and everything is executed imperatively (even declarations)
A ruby feature is that Succinct and flexible _____ that minimizes ______________ and serves as a foundation for _______________ languages.
Succinct and flexible syntax that minimizes syntactic noise and serves as a foundation for domain-specific languages
Ruby features: Dynamic _________ and ________ of objects to facilitate ______________
Dynamic reflection and alteration of objects to facilitate metaprogramming
Ruby features: ___________, _________ and _________, with a unique block syntax
•Lexical closures, iterators and generators, with a unique block syntax
Ruby feature: Literal notation for ______, ______, ________________ and _______
Literal notation for arrays, hashes, regular expressions and symbols
Ruby feature: •Embedding code in _______(interpolation)
Ruby feature: •Embedding code in strings (interpolation)
Ruby features: _______arguments
Default arguments
Ruby features: •________ collection
•Garbage collection
Ruby features: •Four levels of variable scope (_____, ______, __________, and ______) denoted by sigils or the lack thereof
•Four levels of variable scope (global, class, instance, and local) denoted by sigils or the lack thereof
Ruby features: Strict ________ rules (everything is _____except ____and ___)
Strict boolean coercion rules (everything is true except false and nil)
Ruby features: __________ continuations
First-class continuations
Ruby features: ________ handling
•Exception handling
Ruby features: ________ overloading
Operator overloading
Ruby features: Built-in support for ________numbers, ________ numbers and ________ arithmetic
Built-in support for rational numbers, complex numbers and arbitrary-precision arithmetic
Ruby features: Custom dispatch behavior (through : ________and : ________)
Ruby features: Custom dispatch behavior (through method_missing and const_missing)
Ruby features: Native ________ and cooperative ________ (________are 1.9/YARV feature)
•Native threads and cooperative fibers (fibers are 1.9/YARV feature)
Ruby features: Initial support for ________ and multiple character ________ (no ICU support)
•Initial support for Unicode and multiple character encodings (no ICU support)
Ruby features: ________on all major platforms
•Implemented on all major platforms
Ruby features:Native plug-in ____in __
•Native plug-in API in C
Ruby features:Large standard ________
•Large standard library
Ruby deviations:_________ evaluation of non-boolean data is strict: 0, “” and [] are all evaluated to true.
Boolean evaluation of non-boolean data is strict: 0, “” and [] are all evaluated to true.
–Only false and nil are FALSE
Ruby deviations: To denote a _________ without a decimal component, one must follow with a ________ (99.0) or an explicit conversion (____). It is ________ to append a dot (99.) since numbers are susceptible to _____ _____.
•To denote a floating point without a decimal component, one must follow with a zero digit (99.0) or an explicit conversion (99.to_f). It is insufficient to append a dot (99.) since numbers are susceptible to method syntax.
Variables always hold _________ to objects.
Variables always hold references to objects.
Ruby is object-oriented: every value is an ______, including classes and ________ of types that many other languages designate as ______ (such as integers, booleans, and “null”).
•Ruby is object-oriented: every value is an object, including classes and instances of types that many other languages designate as primitives (such as integers, booleans, and “null”).
Every function is a _____ and ______ are always called on an ______.
•Every function is a method and methods are always called on an object.
Ruby features: Centralized package management through ________
•Centralized package management through RubyGems
What are expressions in ruby?
•Expressions are Objects and return values
Methods defined at the top level _____ become members of the _____ class. Since this class is an ancestor of every other class, such methods can be called on any object.
•Methods defined at the top level scope become members of the Object class. Since this class is an ancestor of every other class, such methods can be called on any object.
Ruby outputs: All _______ also have an “inspect” method
–Includes list of _____ variables
•All Object’s also have an “inspect” method
–Includes list of instance variables
Ruby outputs: All Object’s have a “to_s” ______
And you probably want to define one for your _____
•All Object’s have a “to_s” method
–And you probably want to define one for your objects
Ruby methods: Has a “______” statement
Has a “return” statement
Ruby methods: Take ________
Take arguments
Ruby methods: Between “___” “___” pairs
•Between “def” “end” pairs
Ruby methods: But also returns last _________ ________
But also returns last expression evaluated
Creating and Extending Objects in Ruby
obj = Object.new
def obj.talk
puts “I am an object”
end
obj.talk
Objects have Identifiers
s1 = “Hello”
s2 = “Hello”
p s1.object_id
p s2.object_id
What does this output?
s1 = “Hello”
s2 = “Hello”
p s1.object_id
p s2.object_id
$ ruby ref.rb
70235101628760
70235101628740
Passing Parameters
obj = Object.new
def obj.c2f(c)
c * 9.0 / 5 + 32
end
p obj.c2f(100)
p obj.c2f 100
What does this output?
obj = Object.new
def obj.c2f(c)
c * 9.0 / 5 + 32
end
p obj.c2f(100)
p obj.c2f 100
$ ruby ref.rb
- 0
- 0