I'm writing a blog post on how almost everything is an object in Ruby, and I'm trying to show this through the following example:
class CoolBeans
attr_accessor :beans
def initialize
@bean = []
end
def count_beans
@beans.count
end
end
So from looking at the class we can tell it has 4 methods(unless of course, I'm wrong):
attr_accessor
)attr_accessor
)However, when I ask the class itself what instance methods it has, I don't see the default initialize
method:
CoolBeans.new.class.instance_methods
# => [:beans, :beans=, :count_beans, :lm, :lim, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :to_s, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :==, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
Does this mean that the initialize method is not an instance method? If not, why isn't it showing up as a method available to the class CoolBeans
?
In Ruby, a method provides functionality to an Object. A class method provides functionality to a class itself, while an instance method provides functionality to one instance of a class. We cannot call an instance method on the class itself, and we cannot directly call a class method on an instance.
Bound method (instance call): To call the method we must provide an instance object explicitly as the first argument. In other words, a bound method object is a kind of object that remembers the self instance and the referenced function. So, a bound method may be called as a simple function without an instance later.
We can define default argument. It will always return a new object so return keyword is not used inside initialize method. Defining initialize keyword is not necessary if our class doesn't require any arguments. If we try to pass arguments into new and if we don't define initialize we are going to get an error.
instance_methods
returns an array of public and protected methods. However, initialize
is automatically private ref.
CoolBeans.private_instance_methods
# => [:initialize, :default_src_encoding, :irb_binding, :initialize_copy, :initialize_dup, :initialize_clone, :sprintf, :format, :Integer, :Float, :String, :Array, :Hash, :warn, :raise, :fail, :global_variables, :__method__, :__callee__, :__dir__, :eval, :local_variables, :iterator?, :block_given?, :catch, :throw, :loop, :respond_to_missing?, :trace_var, :untrace_var, :at_exit, :syscall, :open, :printf, :print, :putc, :puts, :gets, :readline, :select, :readlines, :`, :p, :test, :srand, :rand, :trap, :load, :require, :require_relative, :autoload, :autoload?, :proc, :lambda, :binding, :caller, :caller_locations, :exec, :fork, :exit!, :system, :spawn, :sleep, :exit, :abort, :Rational, :Complex, :set_trace_func, :gem, :gem_original_require, :singleton_method_added, :singleton_method_removed, :singleton_method_undefined, :method_missing]
# ^^^^^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With