Ruby is fully object oriented language. In ruby, everything is an object & therefore, belongs to some class. for example 5
belongs to Object 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
so, we have to call all methods by prefixing class/object name as in Object_name#method_name
. example:
5.times{|i| puts i}
now, rails has these so called helpers like stylesheet_link_tag
, javascript_include_tag
, form_for
etc which do follow this Object_name#method_name
syntax, so i guess they are just normal functions.
so my question is
everything a object & there are no primitives
.
As the example, people cite 5.+(6)
saying even operators are just
plain methods?For other things called without a receiver, look at the Kernel
module, where stuff like puts
is defined. Since the module is included in Object
, its methods are available everywhere. How exactly would that contradict the -- IMHO overused -- claim that everything is an object?
These rails assets tag helpers are a sub module of ActionView, ActionView::Helpers::AssetTagHelper
which provides methods for generating HTML that links views to assets such as images, javascripts, stylesheets, and feed.
As modules
have class
as a superclass it mean that the AssetTagHelpers will also have it
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
NOTE: For sake of simplicity I will only focus on the JavascriptIncludeTag but they are all pretty similar.
Here you will find a class called ActionView::Helpers::AssetTagHelper::JavascriptIncludeTag
Which you can be instantiate
JavascriptIncludeTag.new(config, asset_paths)
The JavascriptIncludeTag class has a method called asset_tag
which then calls a content_tag
method and returns the correct 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
path: /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
So that is more less how they work.
NOTE: If you found my explanation a bit tricky just let me know and I'll edited to provide a better explanation
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