Class: Object
Overview
Object is the root of Ruby's class hierarchy. Its methods are available to all classes unless explicitly overridden.
Object mixes in the Kernel module, making the built-in kernel functions globally accessible. Although the instance methods of Object are defined by the Kernel module, we have chosen to document them here for clarity.
In the descriptions of Object's methods, the parameter symbol refers to a symbol, which is either a quoted string or a Symbol (such as :name).
Class Method Summary (collapse)
-
+ at_exit { ... }
Converts block to a Proc object (and therefore binds it at the point of call) and registers it for execution when the program exits.
-
+ eval
Evaluates the Ruby expression(s) in string.
-
+ load
Loads and executes the Ruby program in the file filename.
-
+ syscall
Calls the operating system function identified by num and returns the result of the function or raises SystemCallError if it failed.
-
+ warn
Display the given message (followed by a newline) on STDERR unless warnings are disabled (for example with the -W0 flag).
Instance Method Summary (collapse)
-
- Complex
Returns x+i*y;.
-
- to_enum
Creates a new Enumerator which will enumerate by on calling method on obj.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Proc) at_exit { ... }
Converts block to a Proc object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.
def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
produces:
goodbye cruel world
+ (Object) eval(string[, binding [, filename [,lineno]]])
Evaluates the Ruby expression(s) in string. If binding is given, which must be a Binding object, the evaluation is performed in its context. If the optional filename and lineno parameters are present, they will be used when reporting syntax errors.
def get_binding(str)
return binding
end
str = "hello"
eval "str + ' Fred'" #=> "hello Fred"
eval "str + ' Fred'", get_binding("bye") #=> "bye Fred"
+ (true) load(filename, wrap = false)
Loads and executes the Ruby program in the file filename. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program's global namespace. In no circumstance will any local variables in the loaded file be propagated to the loading environment.
+ (Integer) syscall(num[, args...])
Calls the operating system function identified by num and returns the result of the function or raises SystemCallError if it failed.
Arguments for the function can follow num. They must be either String objects or Integer objects. A String object is passed as a pointer to the byte sequence. An Integer object is passed as an integer whose bit size is same as a pointer. Up to nine parameters may be passed (14 on the Atari-ST).
The function identified by num is system dependent. On some Unix systems, the numbers may be obtained from a header file called syscall.h.
syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
produces:
hello
Calling syscall on a platform which does not have any way to an arbitrary system function just fails with NotImplementedError.
Note |
syscall is essentially unsafe and unportable. Feel free to shoot your foot. DL (Fiddle) library is preferred for safer and a bit more portable programming. |
+ (nil) warn(msg)
Display the given message (followed by a newline) on STDERR unless warnings are disabled (for example with the -W0 flag).
Instance Method Details
- (Numeric) Complex(x[, y])
Returns x+i*y;
- (Object) to_enum(method = :each, *args) - (Object) enum_for(method = :each, *args)
Creates a new Enumerator which will enumerate by on calling method on obj.
method |
the method to call on obj to generate the enumeration |
args |
arguments that will be passed in method in addition to the item itself. Note that the number of args must not exceed the number expected by method |
Example
str = "xyz"
enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122
# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)