Had a real revelation or two last night about modules, classes, objects, and methods and their relations to each other. Also got some insight about setting and getting the value of instance variables as well as on the concept of records as hashes and symbols as recordIDs.
Notes following are based on Agile Web Development With Rails pp. 66-67:
class ClassName < ModuleName::ClassNameInPrecedingModule
#declarations about entire class
#(module "has _many" is inhereted (i.e."<")
#from ModuleName)
has_many :symbols #used as hash(records) keys (recordIDs)
#class method abut the class in general
#can be called anywhere in application
#ClassName.method_name
def self.method_name
self.method(params)
end
#another_method uses
#symbols declared above
def another_method
instance variables declared/etc.
symbols.method {|blockvariable|
block
}
end
------------------------
#Instance variables are not directly accessible outside the class.
#To make them available, write methods that return their values:
class NewClassName
#initialize = what happens when new instance of
#NewClassName is generated via the “new” method
#i.e. object_variable = NewClassName.new(param)
#sets the @instance_variable to the param
def initialize(param)
@instance_variable = param
end
#object_variable.method_to_return_value_of_instance_variable
#=>puts the param passed
#from initialize (new) method or the
#new_method_to_set_value_of_instance_variable
def method_to_return_value_of_instance_variable
@instance_variable
end
#object_variable.new_method_to_set_value_of_instance_variable=(param)
#sets the @instance_variable to the param
def new_method_to_set_value_of_instance_variable=(param)
@instance_variable = param
end
end
No comments:
Post a Comment