Is Ruby a fully object oriented language?

Ruby is a fully object oriented language . In ruby, everything is an object and, therefore, belongs to a certain class. for example 5belongsObject class

1.9.3p194 :001 > 5.class
 => Fixnum 
1.9.3p194 :002 > 5.class.superclass
 => Integer 
1.9.3p194 :003 > 5.class.superclass.superclass
 => Numeric 
1.9.3p194 :005 > 5.class.superclass.superclass.superclass
 => Object 
1.9.3p194 :006 > 5.class.superclass.superclass.superclass.superclass
 => BasicObject 
1.9.3p194 :007 > 5.class.superclass.superclass.superclass.superclass.superclass
 => nil 

therefore, we must call all methods by prefixing the name of the class / object, as in Object_name#method_name. Example:

5.times{|i| puts i}

Now the rails are so-called assistants, such as stylesheet_link_tag, javascript_include_tag, form_for, etc., that follow this syntax Object_name#method_name, so I think it's normal functions.

therefore my question

  • What are these rail assistants?
  • . - , everything a object & there are no primitives. 5.+(6), , ?
+5
3

, , Kernel, , puts. Object, . - - , ?

+8

ActionView, ActionView::Helpers::AssetTagHelper, HTML, , , javascripts, .

modules class , , AssetTagHelpers

irb(main):016:0> ActionView::Helpers::AssetTagHelper
=> ActionView::Helpers::AssetTagHelper
irb(main):017:0> ActionView::Helpers::AssetTagHelper.class
=> Module
irb(main):018:0> ActionView::Helpers::AssetTagHelper.class.superclass
=> Object
irb(main):019:0> ActionView::Helpers::AssetTagHelper.class.superclass.superclass
=> BasicObject
irb(main):020:0> ActionView::Helpers::AssetTagHelper.class.superclass.superclass.superclass
=> nil

. JavascriptIncludeTag, .

ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag

JavascriptIncludeTag.new(config, asset_paths) 

JavascriptIncludeTag asset_tag, content_tag .

path:/actionpack/lib/action_view/helpers/asset_tag_helpers/javascript_tag_helpers.rb

require 'action_view/helpers/asset_tag_helpers/asset_include_tag'

# NOTE: on the 'action_view/helpers/asset_tag_helpers/asset_include_tag' it requires '/actionpack/lib/action_view/helpers/tag_helper.rb' so now all this files are connected :)
.
.
.
def asset_tag(source, options)
  content_tag("script", "", { "src" => path_to_asset(source) }.merge(options))
end

:/actionpack/lib/action_view/helpers/tag_helper.rb

def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    content_tag_string(name, capture(&block), options, escape)
  else
    content_tag_string(name, content_or_options_with_block, options, escape)
  end
end

, .

. , , , .

+5

When you see a method call without an explicit receiver, the receiver self. Objects can receive methods in many ways. One, the most obvious, is when you define the methods yourself. Then you can also enable modules.

class Person
  # this adds a bunch of methods to Person, making it a Mongoid model
  include Mongoid::Document
end

The modules you include and the classes you inherit can acquire functionality in the same way.

So, when you see a method without a receiver, think, "what is there selfat this stage? What is its class? What methods has it defined? What modules does it include?" You will learn a lot of interesting things about rubies and rails. :)

+4
source

All Articles