This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/helpers/application_helper.rb | |
module ApplicationHelper | |
def javascript_modules_tag | |
paths = Dir[ | |
Rails.root.join("app/assets/javascripts/modules/**/*"), | |
Rails.root.join("lib/assets/javascripts/modules/**/*"), | |
Rails.root.join("vendor/assets/javascripts/modules/**/*") | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Example</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<%= stylesheet_link_tag 'application', media: 'all' %> | |
<%= csrf_meta_tags %> | |
</head> | |
<body> | |
<%= yield %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascripts/main.js | |
require([modules.lodash,], function (_) { | |
console.log(_); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascripts/modules/example.js | |
define([modules.lodash], function (_) { | |
return "This is an example."; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app/assets/javascripts/application.js | |
//= require jquery | |
//= require jquery-ujs | |
//= require main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define original class | |
class Example | |
def say_hello | |
puts "hello" | |
end | |
end | |
# re-open the class and monkeypatch some of its existing functionality |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define original class | |
class Example | |
def say_hello | |
puts "hello" | |
end | |
end | |
# re-open the class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a namespace to avoid naming collisions | |
module Example | |
class String | |
def length | |
100 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add a kernel method to make it available to all objects | |
# this example also serves to illustrate that everything in Ruby is an object | |
module Kernel | |
def say_hello | |
puts "hello from #{self.class.name}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add methods that provide the ability | |
# to dynamically call unknown methods on objects | |
def invoke(object, method_name) | |
object.send(method_name) | |
end | |
def invoke_with_args(object, method_name, *args) | |
object.send(method_name, *args) | |
end |