Remix.run Logo
kazinator 11 hours ago

Answerable by a few minutes of googling. Sort of:

puts is a method which has a class: the Method class:

  irb(main):001:0> method(:puts)
  => #<Method: main.puts>
  irb(main):002:0> method(:puts).class
  => Method
Everything being a confused muddle in Ruby, there is evidently some Kernel base class that is injected into every Object, and puts is a private method in that:

  irb(main):003:0> 3.puts()
  Traceback (most recent call last):
          2: from /usr/bin/irb:11:in `<main>'
          1: from (irb):3
  NoMethodError (private method `puts' called for 3:Integer)
The Method class of puts is a real class with methods and all:

  irb(main):004:0> method(:puts).class.methods
  => [:allocate, :superclass, :<=>, :<=, :>=, :==, :===, :autoload?, :autoload, :included_modules, :include?, :name, :ancestors, :attr, :attr_reader, :attr_writer, :attr_accessor, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :public_constant, :freeze, :inspect, :deprecate_constant, :private_constant, :const_missing, :singleton_class?, :prepend, :class_exec, :module_eval, :class_eval, :include, :<, :>, :remove_method, :undef_method, :alias_method, :protected_method_defined?, :module_exec, :method_defined?, :public_method_defined?, :to_s, :public_class_method, :public_instance_method, :define_method, :private_method_defined?, :private_class_method, :instance_method, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :instance_variables, :method, :public_method, :singleton_method, :define_singleton_method, :public_send, :extend, :to_enum, :enum_for, :pp, :=~, :!~, :eql?, :respond_to?, :object_id, :send, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :yield_self, :taint, :tainted?, :untrust, :untaint, :trust, :untrusted?, :methods, :frozen?, :protected_methods, :singleton_methods, :public_methods, :private_methods, :!, :equal?, :instance_eval, :instance_exec, :!=, :__send__, :__id__]
WJW 11 hours ago | parent | next [-]

It's not as much a mystery as you think, the Kernel module is pretty well documented and states right in the first paragraph that its methods are available in all Ruby objects: https://ruby-doc.org/core-3.0.2/Kernel.html

cortesoft 11 hours ago | parent | prev [-]

> Everything being a confused muddle in Ruby

It really isn’t a confused muddle, the rules are very clear. Just because it doesn’t match what you expect from your other language experience doesn’t mean it isn’t clear.

pxc 10 hours ago | parent | next [-]

I'm not a Rubyist really, but I started using it at work a few weeks ago for a very small script. I'm not allergic to RTFM, so I picked up the canonical reference book on Ruby and occasionally visit the official docs for the language. I agree: the structure is clear and straightforward and there's nothing difficult about learning it.

It's also the opposite of magic; magic is when language features can't be described in terms of the language itself.

kazinator 6 hours ago | parent | prev [-]

Clear, like the glass in Liberace's candelabra.