Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't initialize appear as a method when I call on the instance methods of a class?

Tags:

ruby

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):

  1. It can initialize a default empty array of beans when a new instance is created
  2. It can count how many beans it has
  3. It can read how many beans it had (through the attr_accessor)
  4. It can write(or add) more beans to the empty array (also through the 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?

like image 300
Becca Avatar asked Sep 10 '15 15:09

Becca


People also ask

Can you call an instance method in a class method Ruby?

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.

How do you call an instance method in Python?

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.

Is it possible to define initialize method without passing any argument?

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.


1 Answers

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]
#     ^^^^^^^^^^^ 
like image 161
Yu Hao Avatar answered Sep 22 '22 10:09

Yu Hao