The beginning of our point and click glossary to ruby - we'll see how it goes. 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 |
map | Core API | The map method is an alias for collect and is available for Array, Set and Enum objects (through Emunerable class). It calls a block for each element in the object and outputs it to a new object of the same type, for example if processing an Array the results of the block operation are placed in a new array whereas with the each method the block has to action the results. )# map (collect) method with Array arr = [1,2,3] res = arr.map {|x| x*x} # => [1,4,9] # functionally the same as arr = [1,2,3] res = [] arr.each {|x| res << x*x } |
member? | Hash | Alias of has_key? include? and key?. |
method | Core API | Method format (as opposed to function format)
# method format obj = Thing.new obj.mymethod(p1) # function format (e.g. to private method) # no object reference mymethod(p1) |
methods | Core API | Methods may be either Class or instance methods and may be methods may be private, public or protected.
# instance method class Ourclass def ourmethod do something end end # class method class Ourclass def Ourclass.ourmethod do something end end # OR (still works if class name changes) class Ourclass def self.ourmethod do something end end |
mixins | Core API | Term that refers to additional methods added to a class by use of the include statement. Mixins are defined inside module statements. |
module | Core API | Module is one of those unfortunate terms that has a well-established connotation (as a packaging or compilable unit) that really (or very vaguely) have nothing to do with Ruby's use of the term. The use of Module Name ...end encloses one or more methods that may be included in a class as what is called a mixin many basic features of Ruby's object classes derive a lot of their methods from the use of mixins. Multiple modules ... end constructs may be defined in a single entity that we would typically call a module or Ruby file if you prefer to try and disambiguate. Mixins can also be used to extend a class or instance:
module Something def some_method do thing end end # class Thing - may be defined in this file or another class Thing include Something # includes the module # and its methods as mixins # instance methods def ... end end |
names | Core API | ruby names may be taken ONLY from a-z, A-Z, 0-9 and '_'. They may be preceded by @, $, % etc to denote special cases. |
not | Core API | condition 1 and NOT condition 2 alias ! |
or | Core API | condition 1 OR condition 2 alias || (precedence higher) |
p | IO or Kernel | Outputs using object.inspect (which normally calls .to_s) method (uses $ globals) to stdout. |
pp | pp | Pretty print version of p allows formated output using object.inspect. Customized class print can be created by passing the pp object to a user class.
# normal usage pp obj # OR pp(obj) |
predictate | - | Predictate methods are defined BY CONVENTION to return a boolean e.g. hash.key?(key). They are defined to be methods and cannot (in 1.6.8) be used in attributes
# predicate method def is_my_key_ok? hash.key?(mykey) end # predicates must be methods and do not work # with attribute accessors # the following code does NOT work class Thing attr_reader :missing? def initialize @missing = false end end t = Thing.new t.missing? # does NOT return false and #does not fail with syntax error |
IO or Kernel | Outputs to $defout ($>) or ios. Uses globals $\(at line end) and $,(between arguments) if non-nil to format. If non-string object calls to_s method. If no argument outputs $_ (last line read by gets or readline) | |
protected | Core API | See also access control for usage. Defines a method is protected - it can only be executed from within the class (any instance) and any subclass (or any instance).
# uses a method format # from within the class class Thing protected # defines the following method or methods until # # next access control directive (public, private, protected) # # appears before the definition def mpm(num) do something end # OR can be defined using a listing format after definition protected :mpm # must be after def end self.mpm(1) # can be anywhere in class |
private | Core API | See also access control for usage. Defines a method to be private - can only be executed from within the class instance and any subclass of that instance
#uses a function format # from within the class def mpm(num) do something end private :mpm # must be after def mpm(1) # can be anywhere in class |
public | Core API | See also access control for usage. Defines a method to be public (default) - can be executed from anywhere |
putc | IO or Kernel | Outputs single char to ?. |
puts | IO or Kernel | Outputs to stdout. |
regex | Core API | Regular expressions (see also regular expression guide.
# simple format delimited by // /search string/ # keyword-value pair pattern = /(\w+)\s*=\s*(.*?)$/ line = "mine = yours " result = pattern.match(line) result[0] = "mine" result[1] = "yours"Notes: Ruby 1.6 was quite relaxed about accepting regexp but 1.8 seems to demand the use of Regexp object when using =~ i.e. use: regexp = Regexp.new(string-expression) if joe =~ regexp then something end |
rescue | Core API | used to trap Exceptions with begin:
begin ... some buggy code ... # rescues all errors rescue {code block} # or rescue only [errortype] e.g StandardError rescue [errortype] {code block} # display any text associated rescue StandardError => ouch print ouch ouch.backtrace.each {|x| print x} # optional statement always runs before # exiting begin section ensure {code block} end |
Resolv.rb | STD-LIB | A DNS reslover library - horribly complicated and incomplete some primitive documentation and usage examples |
reserved | Core API | The following are reserved and should not be used as method or variable names:
__FILE__ __LINE__ alias and BEGIN begin break case class def defined? do END end else elsif ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield |
return | Core API | return result from a method/function - mostly syntax sugar - returns the last result automagically:
# a method def mm return @marray[0] # same as @marray[0] end # or condition setting returns true or false def test(thing) thing >= 5 end # need return when # multiple values are returned def add(one) return one+1, one+2, one+3 end # use all returns two, three, four = add(one) # use some returns - ignore last two, three = add(one) |
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. |