Today's Ruby lessons

Posted by Roy Hooper Fri, 03 Mar 2006 06:02:00 GMT

Today, I learned a few handy things about Ruby.

  • Ruby calls class properties "instance variables" and prefixes them with @
  • Ruby calls class static variables "class properties" and prefixes them with @
  • You can call static methods (class methods) with either .method() or ::method(), but should use ::method() for readability.
  • Class methods are defined by prefixing the method name with the class (eg Test.static_method).
  • Ruby lets you add or redefine methods to classes at any time. Declaring a new class block doesn't destroy the existing class definitions.
  • Object::constants, ::methods, ::class_variables, and other similar methods let you discover what is and is not yet defined with ease. See my previous blog about IRB and tab completion to get the most out of this tidbit.
  • The .respond_to? method is the way to tell if a method is defined for a particular object.
  • The collect method of an array is like map, but not quite. It is like the each method, but it collects the output of the block into a new array.
  • the p() method prints stuff out such as arrays/hashes in a more useful manner than print. For example:

    irb(main):070:0> p Object::methods
    ["to_a", "respond_to?", "ancestors", "module_eval", "const_missing", "type", "protected_methods", "instance_methods", "public_method_defined?", "superclass", "eql?", "instance_variable_set", "const_get", "is_a?", "autoload", "hash", "send", "to_s", "class_eval", "class_variables", "class", "public_instance_methods", "instance_method", "tainted?", "private_methods", "private_method_defined?", "__send__", "included_modules", "untaint", "const_set", "id", "inspect", "instance_eval", "clone", "public_methods", "protected_instance_methods", "protected_method_defined?", "extend", "autoload?", "freeze", "public_class_method", "display", "allocate", "__id__", "<=>", "<", "method", "methods", "==", "===", ">", "nil?", "dup", "instance_variables", "include?", "private_instance_methods", ">=", "instance_of?", "const_defined?", "<=", "name", "private_class_method", "new", "object_id", "=~", "singleton_methods", "method_defined?", "equal?", "taint", "constants", "frozen?", "instance_variable_get", "kind_of?"]
    => nil

    irb(main):071:0> print Object::methods
    to_arespond_to?ancestorsmodule_evalconst_missingtypeprotected_ methodsinstance_methodspublic_method_defined?superclasseql?instance _variable_setconst_getis_a?autoloadhashsendto_sclass_evalclass_ variablesclasspublic_instance_methodsinstance_methodtainted? private_methodsprivate_method_defined?__send__included_ modulesuntaintconst_setidinspectinstance_evalclonepublic_ methodsprotected_instance_methodsprotected_method_defined? extendautoload?freezepublic_class_methoddisplayallocate__id__<=>< methodmethods=====>nil?dupinstance_variablesinclude?private_ instance_methods>=instance_of?const_defined?<=nameprivate_ class_methodnewobject_id=~singleton_methodsmethod_defined? equal?taintconstantsfrozen?instance_variable_getkind_of?=>
    nil
Trackbacks

Use the following link to trackback from your own site:
http://blog.royhooper.ca/trackbacks?article_id=9