The beginning of our point and click glossary to ruby. If the item below has got a link read it - otherwise put up with our definition. You have been warned!
Select from the range below:
Symbols ?, #, ::, etc. | a - d | e - l | m - r | s - z
term | parent | description |
access control | Core API | public, protected, private
# either use as section separator (no parameter) public protected private # terminate section with 'end' # OR with symbol parameter # must come AFTER the method definition public :method1, :method2 # etc protected :method3, :method4 # etc private :method5, :method6 # etc # for Class methods use private_class_method |
accessors | Core API (class Module) | Ruby uses the term accessors to access what are sometimes called class attributes and provides shortcut syntax to define them:
attr_reader :thing #get only attr_writer :thing #set only attr_accessor :thing #get and set attr: thing [,true] # short form of attr_accessor # the last form with the optional [,true] is meant to indicate # it provides attr_accessor proeprties otherwise attr_reader # properties - testing suggests this is not the case and # that in all cases attr_accessor characteristics are provided. # OR roll-your-own def thing # get do something # compute response OR @thing # return stored value end def thing=(thingie) # set do something to thingie # compute value OR @thing = thingie end attr_writer and attr_accessor both create an instance variable of the form @thing if not defined but which is obviously(!) only valid after it has been set. The form attr :thing, true is equivalent to attr_accessor :thing |
alias | Core API |
# either use as section separator (no parameter) alias alias_name current_name # must come AFTER the current_name method |
and | object | condition 1 AND condition 2 alias && (but which has higher precedence than 'and') |
array | Core API | Arrays automatically get Enumerable methods as Mixins. Arrays may be initialized any of the followng means:
ar = Array.new() # empty array ar = [] # empty array # optional size, default value arguments # default value is returned for any element which # has not been assigned ar = Array.new(2, "a") # ["a", "a"] ar = Array.new(2) # [nil, nil] # initialising with values ar = ["a", "a"] ar = Array.[]("a", "a") ar = Array["a", "a"] # mixed values ar = [1, "a", 2, 3] ar << 1 # => [1, "a", 2, 3, 1] |
assignment | Core API | Assignment is an expression
# multiple assignment - same value a = b = 1 # multiple assigment - multiple values a,b,c = "one", "two", "three" # using C style conditional expression to set value msg = ok.empty? ? "yes" ; "no" # condition result to set value msg = if ok.empty? then "yes" else "no" end |
attributes | Core API | Ruby provides support for automatically handling attributes using accessors. |
begin | Core API | Lowercase. The 'begin-end' statements enclose an exception handling block. 'def' has implicit 'begin-end'.
... begin # buggy code! .. rescue SomeError some code end # def has implicit begin-end so this works def method buggy code! ... rescue SomeError some code endNOTE: the keyword BEGIN (uppercase) starts a code block executed first |
BEGIN | Core API | Uppercase. Optional code block enclosed in braces that is executed when the program is loaded/started. May have a corresponding END block which is executed last.
BEGIN {code block}NOTE: the keyword begin (lowercase) starts an exception sequence |
block_given? | Kernel | All method calls will accept an optional block - block_given? allows its presence to be tested either to conditionally generate a result or to avoid an exception:
# method generates one of two results depending # on presence or absence of block def somemethod(p1,p2) if block_given? yield(p1,p2) else p1 + p2 end # call with only arguments somemethod(2,3) # => 5 # call with block somemethod(2,3) {|x,y| x*x + y*y} # => 13 |
class | object | The class statement defines the beginning of a class, terminated with a normal end statement. Most classes have a constructor method (initialize) but this is not a requiremement. Methods within the class may be instance methods or class methods. A class may be used to extend an existing class. Class names are constants and therefore must start with a Capital letter. mixins can be used to extend class methods:
# simple class definition class Thing ... end # class extends (includes all methods of) Another class class Thing < Another |
chr | object | converts a number to a string e.g. 111.chr = "o". Example:
# test for single char in string if string[0].chr == "a" then do something end # OR functionally identical if string[0] == "a"[0] then do something end |
closed? | Class IO | returns true if a file is closed (false if not) UNLESS the file was automatically closed by an iterator function in which case the file reference no longer exists and ruby/mod_ruby will throw an exception. |
comparisons | object | and (synonym &&) or (synonym ||) not (synonym !) == Test for equal value. === Used to test equality within a when clause of a case statement. <=> General comparison operator. Returns -1, 0, or +1, depending on whether its receiver is less than, equal to, or greater than its argument. <, <=, >=, > Comparison operators for less than, less than or equal, greater than or equal, and greater than. =~ Regular expression pattern match. eql? True if the receiver and argument have both the same type and equal values. 1 == 1.0 returns true, but 1.eql?(1.0) is false. equal? True if the receiver and argument have the same object id |
collect | Array, Hash, Set | Map is an alias. Returns an array by iterating each element via the code block. Added via enumerable module. Example:
# newarray == other array with 1 added to each element newarray = old array.collect {|a| a+1} |
def | Core API | Defines the beginning of a method (or function if you prefer). Methods can belong to an instance or the class and can be empty (no arguments are passed) or can take one or more arguments. The method definition is terminated by end:
# definition of a empty instance method class Thing def my_method ... end end # instance method with arguments class Thing def another_method arg1, arg2 # or def another_method (arg1, arg2) ... end end # instance method use a = Thing.new a.another_method(p1,p2) # => some result # defining a class method class Thing def self.class_method(arg1,arg2) ... end end # usage of class method Thing.class_method(p1,p2) # => some resultParameters to def can take multiple forms: # empty arguments def somemethod ... end # variable name arguments def somemethod(p1,p2) ... end # method call somemethod(1,2) # or by using an * in the call arguments can be # exploded from an array # the following is functionally identical to above somemethod (*[1,2]) # variable name arguments with defaults # any or all arguments may have defaults # evaluated left to right and may reference preceding # argument values def somemethod(p1,p2=1,p3=p1+1) ... end # array arguments collection # adding * to argument name collects into an array # which must be last or only argument # NOTE: any argument can also be an array, this form just uses # implicit collection of arguments into an array at the # time of the method call def somemethod(p1,*p2) ... end # adds all excess arguments to p2 array above # 2 and 3 below are placed in array somemethod (1,2,3) # p1 = 1, p2 = [2,3] # hash collection of arguments # used as a type of keyword argument def somemethod(p1,p2) .. end # hash collector shortform for p2 above # does not need normal brace syntax somemethod (1,"one" => 1, "two" =>2) # but could also be written as somemethod (1,{"one" => 1, "two" =>2}) # code block argument # adding & before argument indicates code block # all methods can be called with an optional code block # (see here) # this syntax causes a Proc class object to be created # automatically giving access to all Proc methods # example generates one of three results depending # on presence or absence of block and no. of block arguments def somemethod(p1,p2,&p3) if block_given? if p3.arity == 1 # .arity returns count # of arguments in block yield(p1) else yield(p1,p2) end else p1 + p2 end # call with only arguments somemethod(2,3) # => 5 # call with block and two arguments somemethod(2,3) {|x,y| x*x + y*y} # => 13 # call with block and one argument somemethod(2,3) {|x| x*x} # => 4 |
defined? | object | returns nil if 'thing' does not exist (has not been defined). Used to check for presence of method, expression, variable etc. This NOT a method and hence does not return a boolean. Sigh.
defined? thing # => nil does not exist # if it does exist, may return one of: # expression, super, assignment, method, # yield, self, nil, true, false, assignment, # local-variable, local-variable(in-block) # global-variable, instance-variable, constant, # class variable |
Select from the range below:
Symbols ?, #, ::, etc. | a - d | e - l | m - r | s - z
Problems, comments, suggestions, corrections (including broken links) or something to add? Please take the time from a busy life to 'mail us' (at top of screen), the webmaster (below) or info-support at zytrax. You will have a warm inner glow for the rest of the day.
Tech Stuff
If you are happy it's OK - but your browser is giving a less than optimal experience on our site. You could, at no charge, upgrade to a W3C standards compliant browser such as Firefox
Search
Share
Page
Resources
Main Ruby site
The Book
ruby-doc.org
RubyGems
Ruby on Rails
Useful Stuff
Our Pages
Site
Copyright © 1994 - 2024 ZyTrax, Inc. All rights reserved. Legal and Privacy |
site by zytrax hosted by javapipe.com |
web-master at zytrax Page modified: January 20 2022. |