class Dog
def include_mod # not workable
include Mod
end
def self.include_mod #workable
include Mod
end
end
module Mod
def shat
puts "shat"
end
end
dog = Dog.new
dog.class.include_mod
puts Dog.instance_method :shat
dog.include_mod
#'include' is a method belonging to Module
#Class inherit Module, so Class can include
#And 'include' is one of Module's instance methods, so the decleared module Mod has a method include.
#Module is Class, and Class is a Module. Both of them has two set of methods: instance_methods and his owner methods.
instance method 'include' in Class is passed into Dog as regular method when declaration.
but when declear "dog = Dog.new", 'include' is not a instance method any more in Dog. So dog can't include.
分享到:
- 2009-02-26 15:33
- 浏览 1023
- 评论(1)
- 论坛回复 / 浏览 (1 / 1879)
- 查看更多
相关推荐
Ruby 1.9+用法 只需使用您希望其实现对象定义的任何方法创建一个模块module RemoteControl # turns the device on def on end # turns the device off def off endend然后在您的类中使用implements方法(也别名为...
ruby实现石头剪刀布游戏 代码如下:#encoding: utf-8arr = [‘石头’, ‘剪刀’, ‘布’]win_arr = [[‘石头’, ‘剪刀’], [‘剪刀’, ‘布’], [‘布’, ‘石头’]]#随机computer的值,放入result数组中result = ...
安装该gem使用C扩展名从Ruby OpenSSL::SSL::SSLSocket对象提取数据。 这意味着必须使用与编译Ruby解释器相同的OpenSSL标头来构建Gem。 查找include目录的逻辑不是特别复杂,因此在安装过程中可能需要指定正确的位置...
到目前为止,存在以下Elixir模块的部分实现: 代理人 原子 根据 辞典 枚举 文件 漂浮 整数 列表 OptionParser 小路 范围 放 溪流 细绳 系统 任务 元组 版本 要求 Ruby 2.2+ 安装 gem install elixir.rb 例子 ...
sass-include-paths 生成包括流行软件包管理器(例如npm,bower,ruby gem,ruby bundler)的软件包的node-sass路径。 可以将包含路径的生成数组传递给node-sass。 允许在sass变体,ruby sass,libsass和sass眼镜...
透明质酸 这是使用蛋白石的ruby虚拟DOM实现。 它受react.js的启发。例子require 'hyalite'class ExampleView include Hyalite :: Component state :count , 0 def component_did_mount interval = Proc . new do @...
两者都以纳秒级的分辨率运行,达到您的Ruby实现支持的极限。 例如,要测量经过的时间,请创建一个“ Instant以标记起点,执行操作,然后询问自以下时间以来经过的“ Duration : include Monotime start = ...
在ActiveRecord可以用很方便的声明方式来定义model之间的关联关系,例如: 代码如下: class Topic < ActiveRecord::Base has_many :posts belongs_to :user end has_many和belongs_to其实是Topic类的class ...
有两种主要方法可以为您的模型实现验证 1)将带有属性的验证方法作为您的类中的参数 require 'dm-core' require 'dm-validations' class ProgrammingLanguage include DataMapper :: Resource property :name , ...
Nigilist 对于实现空对象模式非常有用。 它允许 null 对象为所有问题方法返回false ,为所有非 bang 方法返回nil为所有复数形式的方法(以及在 Ruby on Rails 中为同名 ActiveRecord 模型提供空关系)返回[] ,而...
Eventful是Ruby的Observable模块之上的一个小扩展,它实现了命名事件,块侦听器和事件冒泡。 它提供比Observable通常允许的更加灵活的事件处理行为,后者通常要求侦听器是实现update对象,并且没有提供基于事件类型...
SystemNavigation是一个Ruby库,它为Ruby程序提供附加的自省功能。 该库定义了许多允许查询的有用方法: 方法,例如,类或全局变量 文字(例如数字,字符串,符号等)的方法。 查找特定字符串的方法(方法源搜索)...
模块实现混合工厂。 模块定义了一个命名空间,一个沙箱中方法和常量可以自由使用,而不必担心踩到其他的方法和常数。 语法: module Identifier statement1 statement2 ........... end 就像被命名为类常量模块...
这就是为什么您必须创建一个类并使用Comparateur include或extend它并根据需要使用它。 此实现还允许您构建自己的缓存系统。 安装 将此行添加到应用程序的 Gemfile 中: gem 'comparateur' 然后执行: $ bundle...
Mongoid搜索Mongoid搜索是Mongoid ORM的一种...在您的Gemfile中安装:gem'mongoid_search'然后:bundle install示例类产品include Mongoid :: Document include Mongoid :: Search字段:brand字段:name字段:unit fie
该dotiw库可将distance_of_time_in_words添加到任何Ruby项目中,或使用更准确的输出覆盖Rails中的默认实现。 您是否想将精度降低到秒? 我也是。这就是为什么我制造了这种宝石。 安装 添加到您的Gemfile 。 gem '...
transitions是Ruby状态机的实现。 安装 滑轨 这进入您的Gemfile: gem "transitions" , :require => [ "transitions" , "active_model/transitions" ] …并将其纳入您的ORM模型: include ActiveModel :: ...
GNU编译器集(其前身为GNU C编译器)诞生于1987年。...日益流行的脚本语言Perl、Python和Ruby,以及正在不断发展的mono 可移植C#实现的确有助于冲淡人们对Linux编程的传统看法,但这完全是另外一个问题了。
加纳 Garner是Ruby和Rack应用程序的缓存层,支持模型和实例绑定以及层次... 接下来,在您的应用程序中包含适当的mixin: 对于普通的Ruby应用程序,请include Garner::Cache::Context 。 对于Rack应用,首先require "g
另一方面,如果你已经花了很大功夫学习 PHP 的话,那么从 Ruby 重新开始又要重头学起。 为 PHP 开发的框架有很多个(大约 40 个),CI 只是其中之一。其它的还包括 Zend Framework、Cake、Trax 等。下列网址可以...