mail us  |  mail this page

contact us
training  | 
tech stuff  | 

Tech Stuff - Ruby Glossary E - L

The beginning of our point and click glossary to ruby - we'll see how it goes. If the item below has a link - read it, otherwise put up with our definition. You have been warned!

Glossary Navigation

Select from the range below:

Symbols ?, #, ::, etc. | a - d | e - l | m - r | s - z

term parent description
each various Allows iteration over the object. Check on the definition for the object for exact behaviour Implementing an each method when the underlying object has an each method. To create an each method:

empty? String, Array, Hash, Set Returns true if the object is empty.
thing = ""
thing.empty? # => true i
thing = []
thing.empty? # => true

# to test if a variable exists 
defined? name # => nil if not defined else a text description.
end Core API general closure statement for almost any construct such as class, def, if, do, while etc.
def my_method (p1)
 ...
end
END Core API Optional code block enclosed in braces that is executed when the program finishes. Has corresponding BEGIN
END {code block}
enumerable Core API The Enumerable module is added as a mixin to Arrays, Hashes and Set objects and adds the followings methods:
collect, detect, each_with_index, entries, find,
# find_all, grep, include?, map, max,
# member?, min, reject, select, sort, to_a
eval Core API evaluates (compiles) a string expression as code. Example:
miobj = "Thing.new"
mithing = eval(miobj)
# is exactly equivalent to
mithing = Thing.new

When all else fails eval usually works. A real life example using FileUtils.rb

exist? Core API
Pathname
StringScanner
File(FileTest)
In the case of File (FileTest) test whether a file or directory exists and returns true if so else false. Pathname usage is exactly the same as File. Stringscanner may return a position (single value) a string (multiple values) or nil (does not exist). Examples:
# File (FileTest) test
File.exist?("/path/to/file/or/directory") # => true if exists

# StringScanner
# test whether a string exists but does not 
# change current string pointer (predictive operation)
s = StringScanner.new('test string')
s.exist? /s/            # -> 3
s.scan /test/           # -> "test"
s.exist? /s/            # -> 6
s.exist? /e/            # -> nil

exists? Core API
FileTest
File
Obsolete method use exist? instead.
exit Core API terminates a Ruby program/script optionally with a status condition by raising the SystemExit exception which may be caught by the script using Rescue statement. Example:
exit [defaults to 0]
exit(0)
# the optional exit condition by POSIX convention
# is 0 = OK (good status) and > 0 indicates an error state.
# The value of the error state is defined by the 
# application/script as a diagnostic aid
exceptions Core API The primary error object. All errors are subclassed from Exception e.g. StandardError. Some have additional properties. Self generated (Kernel#raise or Kernel#fail is alias). Explanation, usage and examples.
# creates an Exception object supplies associated text
raise "ouch"
Code bugs
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}

ensure {code block}
end
extend Object class
Core API
Allows a class to be extended with mixins either within the class or to a specific instance of the class:
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
 extend Something  # methods of Something become class
                   # methods of Thing
 # instance methods of class
 def stuff
  ...
 end
end

# invoke as a class method
Thing.some_method

# using extend for instances
module SomethingElse
 def some_method
  do thing
 end
end

# class Thingie - may be defined in this file or another
class Thingie

 # instance methods
 def ...
 end
end

# adding mixin to an instance of Thingie
a = Thingie.new
a.extend(SomethingElse)
a.some_method # works
b = Thingie.new
b.some_method # fail, no method error
The method last is also aliased by Array class
files IO object
Core API
IO object
 r - Read-only, starts at beginning of file  (default mode).
 r+ - Read-write, starts at beginning of file.
 w - Write-only, truncates existing file
     to zero length or creates a new file for writing.
 w+ - Read-write, truncates existing file to zero length
      or creates a new file for reading and writing.
 a - Write-only, starts at end of file if file exists,
     otherwise creates a new file for writing.
 a+ - Read-write, starts at end of file if file exists,
      otherwise creates a new file for reading and
      writing.
 b - (DOS/Windows only) Binary file mode (may appear with
     any of the key letters listed above).

# file access - close/autoclose
# line level access - no autoclose
mf = File.open("myfile")
mf.each {|x| print x}
f.close # no autoclose

# file level access - autoclose
mf = file.open("myfile") do |file|
	mf.readlines.each {|x| print x}
end
# IO versions always autoclose
IO.foreach("myfile") {|x| print x}
IO.readlines("myfile").each {|x| print x}
ma = IO.readlines("myfile")
first Array synonym for [0] e.g. the following are equivalent (aliased by Array class)
a = myarray.first
a = myarray[0]
The method last is also aliased by Array class
function Core API We, being ex-procedural language guys, use the term function to describe the use of a def statement outside a class definition. Strictly speaking what we call a function is in Ruby terms a private method of class Object. There is essentially no difference between a method and what we procedural guys call a function. Anything defined inside a def ... end is not executed in-line but must be called either as a method e.g. x.y() from outside the class or as a y() from within a class or in-line code:
# class instance method format
class Thing
  def mymethod(px)
    do something
  end
  
  def some(p1)
	 mymethod(p1+1) # call method from within class
  end
end

obj = Thing.new
obj.mymethod(p1)

# function format (private method of class Object)
# or written in-line function
# MUST be defined before call
# separate namespace inside def limits
  ...
  some code
  ...
  # function definition - not executed in-line
  def mymethod(px) # private method
    do something
  end
  ...
  some more code
# function call (call private method)
  mymethod(p)
  ...
globals - Global variables
$SAFE safety level - can be read and written(values)
$& copy of string from last pattern match
$+ The contents of the highest-numbered group 
   matched in the last successful pattern match 
   in "cat" =~/(c|a)(t|z)/, $+ will be 
   set to ``t''.
$` The string preceding the match in the last 
   successful pattern match
$' The string following the match in the last
   successful pattern match
$= If not nil or false, pattern matches will 
   be case insensitive
$1 contents of successive groups matched in
   the last successful pattern match
   In "cat" =~/(c|a)(t|z)/, 
   $1 will be set to ``a'' 
   and $2 to ``t''
$~ An object that encapsulates the results of 
   a successful pattern match variables 
   $&, $`, $', and $1 to $9 are 
   all derived from $~ Assigning to $~  changes 
   the values of these derived variables
$/ The input record separator (newline by default)
   If NIL whole file will be read
$-0 Synonym for $/
$, FS field separator
$\ RS record separator
$_ last line read by gets and readline
$: library path (used by require, load etc)(array)
$; default separator used by String#split
$< alias for ARGF
$! last exception
$@ last stack backtrace
$. line number in current file (last read)
$> destination for print & printf (default 
   value is $stdout)
$defout alias for $>
$-F alias for $;
$stderr
$stdin
$stdout 
$0 program name
$* command line options (alias ARGV)
$" array of 'load'ed items
$$ process number (pid)
$? exit status of last child
$-a true if -a option on cml
$-d alias for $DEBUG
__FILE__ name of current source file
$DEBUG true if -d cml option used
$F array contains lines if -a cml option (split input)
$FILENAME current input filename
$-I alias for $:
$-i name of back-up file
$-K multibyte coding system
$-L true if -l cml option set
__LINE__ current line number
$LOAD_PATH alias = $:
$-p true if -p option on cml
$VERBOSE true if -v option additional diagnostics
$-v alias for $VERBOSE
$-w alias for $VERBOSE
# SPECIAL GLOBALS
ARGF (alias = $<) command line pseudo-file 
     (behaves like IO object)
ARGV (alias = $*) array of command line arguments
DATA  The file object of the script,
#     pointing just after __END__.
ENV   hash of environmental variables
FALSE         false
NIL           nil
RUBY_PLATFORM current platform
RUBY_RELEASE_DATE version release date
RUBY_VERSION  current ruby version
STDIN         Standard input - default for $stdin.
STDOUT        Standard output - default for $stdout.
STDERR        Standard error output - default for $stderr.
has_key? Hash checks for presence of key in hash (aliases key?, member?, include?):
a = {"one" => 1, "two" => 2}
a.has_key?("two") # => true
a.has_key?("three") # => false
has_value? Hash checks for presence of value in hash (alias value?):
a = {"one" => 1, "two" => 2,"three"=> "three"}
a.has_value?(1) # => true
a.has_value?(3) # => false
a.has_value?("three") # => true
id2name Core API method of all symbol objects to print name
Myvar = :myvar
print Myvar.id2name # => "myvar")
if Core API conditional expression - multiple forms:
# single line - then essential in this form
if x == y then print x end
# multi-line - then not essential
if x == y
 print x
end
# multiple tests on multiple lines does not
# need then
if x == y
	do something
elsif x == z
	do somethingelse
else
	get lost
end
# if on single lines does 
# need then
if x == y then do something
elsif x == z then do somethingelse
else then do squat
end

unless can replace if in any expression if it helps readability or makes more sense.

join Core API (Array) creates a string by appending all elements in an Array with an optional separator:
# no separator
arr = ["1","2"]
arr.join # => "12"

# with separator
arr = ["1","2"]
arr.join("-") # => "1-2"
include Module Includes all the methods in a module into a class:
module Something
 def a_method
 end
end

# a_method is available to class Thing
class Thing
 include Something
end
include? Hash Alias of has_key? member? and key?.
include? Module Returns true if module has been included in class:
module A
end

class B
  include A
end

class C < B
end

B.include?(A)   #=> true
C.include?(A)   #=> true
A.include?(A)   #=> false
iterators Core API Two primary forms single and multi-line:
# single line (delimited with braces)
obj.each {|val| something to val}
# multi-line (delimited with end)
obj.each do |val|
  something to val
end
key? Hash Alias of has_key? member? and include?.
keywords Core API See reserved names.
last Array synonym for last element in array e.g. the following are equivalent (aliased by Array class)
a = myarray.last
a = myarray[myarray.size]
The value first is also aliased by Array class

Glossary Navigation

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

RSS Feed Icon

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

web zytrax.com

Share

Icons made by Icomoon from www.flaticon.com is licensed by CC 3.0 BY
share page via facebook tweet this page

Page

email us Send to a friend feature print this page Display full width page Decrease font size Increase font size

Resources

Main Ruby site
The Book
ruby-doc.org
RubyGems
Ruby on Rails

Useful Stuff

Ruby PLEAC

Our Pages

our ruby pages
glossary

Site

CSS Technology SPF Record Conformant Domain
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.