Jim Weirich
Adapted S6/S9 Version 1 from Original Slide Deck
Slide Show Keyboard Controls (Help)
Action | Key |
Go to next slide | Space Bar, Right Arrow, Down Arrow, Page Down, Click Heading |
Go to previous slide | Left Arrow, Up Arrow, Page Up |
Toggle between slideshow and outline view (Ø) | T |
Show/hide slide controls (Ø « ») | C, Move mouse to bottom right corner |
Zoom in, zoom out, zoom reset (100%) | Control+ Plus, Control+ Minus, Control+ 0 |
I used to teach an after hours course in C programming for employees of a large manufacturing company. It was pretty easy to tell what programming languages the students had used previously just by looking at the style of C code they produced. It is certainly true that “You can write FORTRAN in any language”.
Java programmers investigating Ruby will find a language that look similar in many ways. There are classes and modules, namespaces and scopes, instance variables and methods. A Java programmer will feel quite at home in this Object Oriented language.
So the temptation will be to continue to program in a Java-style. Sure, there are some things that are different (the lack of type declarations will probably be the first thing that strikes them). But nothing that can’t be worked around with a little effort … and they will miss a golden opportunity.
The Sapir-Whorf Hypothesis theorizes that thoughts and behavior are determined (or are at least partially influenced) by language. […] To this day it has not been completely disputed or defended, but has continued to intrigue researchers around the world.
A language that doesn’t affect the way you think about programming is not worth knowing – Alan Perlis
This is not yet another “Ruby is better than Java” article. Nor is it intended to bash Java or Java programmers. Rather, it is an attempt to aid Java programmers who are investigating Ruby by helping them quickly get over the “Writing Java in Ruby” syndrome and to discover the Ruby Way.
Now, on to our 10 Things…
ClassNames
method_names
and variable_names
methods_asking_a_question?
slightly_dangerous_methods!
@instance_variables
$global_variables
SOME_CONSTANTS
or OtherConstants
Some of the conventions are enforced by the language, others are merely standards used by the community.
Everything in Ruby that can be bound to a variable name is a full-fledged object.
This has interesting consequences
Array
is a constant name that is bound to the Array class object.Creating new objects does not require special syntax. We just send new
to the class object.
{: .clear}
a = Array.new
Since Classes create instances of themselves, they are the ultimate factory object.
def create_from_factory(factory)
factory.new
end
obj = create_from_factory(Array)
Even integers are full fledged objects.
0.zero? # => true
1.zero? # => false
1.abs # => 1
-1.abs # => 1
1.methods # => list of methods for object 1
2.+(3) # => 5 (same as 2+3)
10.class # => Fixnum
(10**100).class
# => Bignum
nil
is an Object!null
means “no reference to object”null
is an errornil
is a normal objecta = nil
a.nil? # => true
a.methods # => list of methods
a.abs # => NoMethodError
Proc
objects
|
|
All computation in Ruby happens through:
if
/else
, while
) and operators (e.g. defined?
)string.index("x")
:index
(with argument “x
”)string.length
:length
(with no argument)run_status_reports
:run_status_reports
(to self
)1 + 2
:+
(with argument 2
) to the object 1
array[i]
:[]
(with argument i
) to the arrayJava Programmers tend to think of obj.method()
as looking up
a member function in a table and calling it.
Ruby programmers tend to think of obj.method
as sending a message
to an object.
What’s the Difference?
The difference is subtle, but important!
Consider the following class. It defines an object that is able to record all the messages ever sent to it, and then playback those messages to another object.
class VCR
def initialize
@messages = []
end
def method_missing(method, *args, &block)
@messages << [method, args, block]
end
def play_back_to(obj)
@messages.each do |method, args, block|
obj.send(method, *args, &block)
end
end
end
Example Code
require 'src/vcr'
vcr = VCR.new
vcr.sub!(/Java/) { "Ruby" }
vcr.upcase!
vcr[11,5] = "Universe"
vcr << "!"
string = "Hello Java World"
puts string
vcr.play_back_to(string)
puts string
Output
Hello Java World
HELLO RUBY Universe!
One of the big attractions of Java over C++ was the dynamic features of the language. You could easily load classes at run time, query objects about their classes and methods, and even call methods discovered at runtime.
Ruby takes dynamic behavior several steps beyond Java.
|
|
public static Object create(Class c, String value)
throws Exception
{
Constructor ctor = c.getConstructor(
new Class[] { String.class } );
return ctor.newInstance( new Object[] { "Hello" } );
}
public static void main (String args[])
throws Exception
{
Greeting g =(Greeting) create(Greeting.class, "Hello");
g.show();
}
The Ruby Version
def create(klass, value)
klass.new(value)
end
g = create(Greeting, "Hello")
g.show
Methods can be added to classes at any point … even built in classes.
class Integer
def even?
(self % 2) == 0
end
end
p (1..10).select { |n| n.even? }
# => [2, 4, 6, 8, 10]
Caution is advised, but this feature can be very useful.
Singleton methods are defined on individual objects, not classes.
class Dog
end
rover = Dog.new
fido = Dog.new
def rover.speak
puts "Red Rover"
end
rover.speak # => "Red Rover"
fido.speak # => NoMethodError
Hooks allow the user to gain control at interesting moments during the execution of a program.
class MyClass
def MyClass.method_added(name)
puts "Adding Method #{name}"
end
def new_method
# Yada yada yada
end
end
Output
Adding Method new_method
class Module
def trace_attr(sym)
self.module_eval %{
def #{sym}
printf "Accessing %s with value %s\n",
"#{sym}", @#{sym}.inspect
@#{sym}
end
}
end
end
class Dog
trace_attr :name
def initialize(string)
@name = string
end
end
Dog.new("Fido").name # => Accessing name with value "Fido"
What is a Type?
A type is
a set of values
and
a set of operations
C Code (Weak)
|
Java Code (Strong)
|
|
|
Output
|
Output
|
Ruby Code (?)
Output
|
So what makes a language type safe?
Or…
|
Ruby Code
|
Java Code
|
Output
|
Output
|
Java is
typed. |
Ruby is
typed. |
I’ve been a statically typed bigot for quite a few years. I learned my lesson the hard way while using C. Too many systems crashed in the field due to silly typing errors. […]
Four years ago I got involved with Extreme Programming. […] I can’t imagine not having a comprehensive suite of unit tests to back up my development. […]
About two years ago I noticed something. I was depending less and less on the type system for safety. My unit tests were preventing me from making type errors. […]
So I tried writing some applications in Python, and then Ruby. I was not entirely surprised when I found that type issues simply never arose.
Ruby Uses Duck Typing
|
|
No need to inherit from a common interface.
Although Ruby does not have interfaces, it does have mix-ins defined by modules.
A module…
Although all the logic is in the less-than method, all the other comparisons must still be defined.
class Pair
attr_accessor :first, :second
# ...
def <(other)
(first < other.first) ||
(first == other.first && second < other.second)
end
def >(other)
other < self
end
# Other methods defined in terms of less than:
# <=, >=, ==
end
A mix-in allows the commonality to be factored out.
module ComparableUsingLess
def >(other)
other < self
end
# Other methods defined in terms of less than:
# <=, >=, ==
end
class Pair
include ComparableUsingLess
attr_accessor :first, :second
# ...
def <(other)
(first < other.first) ||
(first == other.first && second < other.second)
end
end
Iteration
[1,2,3].each do |item| puts item end
Resource Management
file_contents = open(file_name) { |f| f.read }
Callbacks
widget.on_button_press { puts "Got Button Press" }
ri
is Your Friend, irb
is Your Other Friendri
irb
$ ri Array
---------------------------------------------------------- Module: Array
Arrays are ordered, integer-indexed collections of any object.
Array indexing starts at 0, as in C or Java. A negative index is
assumed to be relative to the end of the array---that is, an index
of -1 indicates the last element of the array, -2 is the next to
last element in the array, and so on.
------------------------------------------------------------------------
Includes:
---------
Enumerable(all?, any?, collect, detect, each_with_index, entries,
find, find_all, grep, include?, inject, map, max, member?, min,
partition, reject, select, sort, sort_by, to_a, zip)
Class methods:
--------------
[], new
Instance methods:
-----------------
&, *, +, -, <<, <=>, ==, [], []=, assoc, at, clear, collect,
collect!, compact, compact!, concat, delete, delete_at, delete_if,
each, each_index, empty?, eql?, fetch, fill, first, flatten,
flatten!, frozen?, hash, include?, index, indexes, indices, insert,
inspect, join, last, length, map, map!, nitems, pack, pop, push,
rassoc, reject, reject!, replace, reverse, reverse!, reverse_each,
rindex, select, shift, slice, slice!, sort, sort!, to_a, to_ary,
to_s, transpose, uniq, uniq!, unshift, values_at, zip, |
# Another RI Example
Ask about the instance method last
…
$ ri Array#last
------------------------------------------------------------- Array#last
array.last => obj or nil
array.last(n) => an_array
------------------------------------------------------------------------
Returns the last element(s) of _self_. If the array is empty, the
first form returns +nil+.
[ "w", "x", "y", "z" ].last #=> "z"
Add 1+2
, then find the methods defined in Proc
…
$ irb --simple-prompt
>> 1 + 2
=> 3
>> Proc.instance_methods(false)
=> ["[]", "==", "dup", "call", "binding", "to_s",
"clone", "to_proc", "arity"]
“I decided to try out Ruby to solve my problem. So I wrote a little code and all of a sudden I discovered that I was done.”
.
” (dot) VS “::
” (double colon)finally
is named ensure
(10) Learn Ruby Conventions
(9) Everything is an Object
(8) (Almost) Everything is a Message
(7) Ruby is Way More Dynamic Than You Expect
(6) Objects are Strongly Typed, Not Statically Typed
(5) Don’t Worry About Interfaces
(4) Mix it up with Mix-ins
(3) Embrace Closures
(2) ri
is Your Friend, irb
is Your Other Friend
(1) Write Less Code
(0) Ruby Makes Programming Fun Again
This presentation is made available under the Creative Commons Attribution/Non-Commercial License, version 2.0. This means you are able to copy, distribute, display, and perform the work and to create derivitive works, under the following conditions: