Ruby

Ruby is a dynamic, reflective, general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby supports multiple programming paradigms, including functional, object-oriented, imperative, and reflection. It also has a dynamic type system and automatic memory management. It is therefore similar in varying respects to Python, Perl, Lisp, etc..

Matsumoto wished to create a language that balanced functional programming with imperative programming. He wants a scripting language that was more powerful than Perl, and more object-oriented than Python.

object-oriented designs, classes with inheritance, mixins, iterators, closures, exception handling, garbage collection.

Ruby is designed for programmer productivity and fund, following the principles of good user interface design. Matsumoto stresses that the system design needs to emphasize hum, rather than computer's needs.

Often people, especially computer enginers, focus on the machines. They think, "By doing this, the machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine will something, something, something." They are focusing on machines. But in fact we need to focus on humans, on how humans care about doing programming or operating the applications. We are the masters. They are the slaves.

  1. Object-oriented (everything is an object)
  2. Five levels of scope (global, class, instance, local, and block)
  3. Exception handling
  4. Iterators and closures (based on passing blocks of code)
  5. Native, Perl-like regular expression at the language level
  6. Operator overloading
  7. Automatic garbage collection
  8. Cooperative multi-threading using green threads
  9. Introspection, reflection, and metaprogramming
  10. Large standard libraries
  11. Support dependency injection
  12. Support object runtime alteration
  13. Continuations and generators

Ruby is object-oriented: every data typ is an object, including classes and types which many other languages designate as primitives. Every function is a method. Named values (variables) always designate references to object, not the object themselves. Ruby supports inheritance with dynamic dispatch, mixins, and singleton methods (belonging to, and defined for, a single instance rather than being defined on the class). Though Ruby does not support multiple inheritance, classes can import modules as mixins.

Procedural syntax is supported, but all methods defined outside of the scope of a particular object are actually methods of the Object class. Since this class is the parent of every other class, the changes become visible to all classes and objects.

Interactive Ruby Shell:

irb

Executing Ruby Script:

ruby filename

Examples:

puts "Hello World!"

# Everything (including literals) is an object, so this works:
-199.abs                              # 199
"ruby is cool".length              # 12
"Rick Astley".index("c")         # 2
"Nice Day Isn't It?".downcase.split(//).sort.uniq.join  # example of chaining and regular expression

puts "What is your favsorite number?"
number = gets.chomp
outputnumber = number.to_i + 1
puts outputnumber.to_s + ' is a bigger and better favorite number.'

# There are a variety of methods of defining strings in Ruby.
a = "\nThis is a double quoted string\n"
a = %Q{\nThis is a double quoted string\n}
a = <<BLOCK
This is a multi-line double quoted string
BLOCK
a = %/\nThis is a double quoted string\n/

a = 'This is a single quoted string'
a = %q{This is a single quoted string}

# Constructing and using array:
a = [1, 'hi', 3.14, 1, 2, [4, 5]]    # nested array
p a[2]
p a.[](2)
p a.reverse
p a.flatten.uniq

# Constructing and using associative array:
hash = { :water => 'wet', :fire => 'hot' }
puts hash[:fire]
hash.each_pair do |key, value|
    puts "#{key} is #{value}"
end
hash.delete :water
hash.delete_if {|k,value| value=='hot'}

# Code blocks
{ puts "Hello World!" }
do puts "Hello World!" end

# Parameter-passing a block to be a closure

Ruby vs Java

page_revision: 6, last_edited: 1230755783|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License