| Deletions are marked like this. | Additions are marked like this. |
| Line 70: | Line 70: |
|
---- CategoryCheatSheet |
As I'm just learning Ruby, I sometimes need to jog my memory. Perhaps this will be useful to others, too. See the [http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html manual] or the [http://www.rubycentral.com/book/ book] for more detail.
Other references
[http://www.zenspider.com/Languages/Ruby/QuickRef.html Ruby QuickRef]
[http://www.glue.umd.edu/~billtj/ruby.html Things That Newcomers to Ruby Should Know]
[http://ruby-pdf.rubyforge.org/pdf-writer/demos/Ruby-Library-QuickRef.pdf Ruby Libarary QuickRef] (2-page PDF)
[http://blog.nanorails.com/pages/rails_1.1_cheat_sheet Rails 1.1 cheat sheet]
Variables and constants
FOO # constant $foo # global @@foo # class @foo # instance foo # local
Arrays
a = [ 1, 'cat', 3.14 ] # array with three elements
Classes
declaration:
class Foo < Super
BAR = "bar"
def initialize(name) # constructor
@name = name
end
def method0
return @name
end
def method1 (arg1)
super arg1
print arg1
end
def Foo.classMethod arg1
return arg1 * arg1
end
protected
def protected1
print "protected method"
end
private
def private1
print "private method"
end
endaccess:
myvar = Foo::BAR # access constant BAR in class Foo myinstance = Foo.new "myname" whatismyname = myinstance.method0
hints and tools
Interactive Ruby Interpreter at [http://www.ruby.ch/en/rubymain.shtml Ruby.CHannel]
telling the difference between a string and an array
An array will return true for var.respond_to?("join")
