`
liano
  • 浏览: 25365 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

从 include 看ruby的实现方式

阅读更多
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.
分享到:
评论
1 楼 Omnibus 2009-02-27  
include予class instance methods
extend予class class methods

相关推荐

    interface:ruby 中的可实现接口

    Ruby 1.9+用法 只需使用您希望其实现对象定义的任何方法创建一个模块module RemoteControl # turns the device on def on end # turns the device off def off endend然后在您的类中使用implements方法(也别名为...

    ruby实现石头剪刀布游戏示例

    ruby实现石头剪刀布游戏 代码如下:#encoding: utf-8arr = [‘石头’, ‘剪刀’, ‘布’]win_arr = [[‘石头’, ‘剪刀’], [‘剪刀’, ‘布’], [‘布’, ‘石头’]]#随机computer的值,放入result数组中result = ...

    sslkeylog:一个以NSS密钥日志格式记录SSL会话密钥的Ruby库

    安装该gem使用C扩展名从Ruby OpenSSL::SSL::SSLSocket对象提取数据。 这意味着必须使用与编译Ruby解释器相同的OpenSSL标头来构建Gem。 查找include目录的逻辑不是特别复杂,因此在安装过程中可能需要指定正确的位置...

    elixir.rb:Elixir标准库各部分的Ruby实现

    到目前为止,存在以下Elixir模块的部分实现: 代理人 原子 根据 辞典 枚举 文件 漂浮 整数 列表 OptionParser 小路 范围 放 溪流 细绳 系统 任务 元组 版本 要求 Ruby 2.2+ 安装 gem install elixir.rb 例子 ...

    sass-include-paths:为流行软件包管理器(例如npm,bower,ruby gem,ruby bundler)的软件包生成node-sass的包含路径。

    sass-include-paths 生成包括流行软件包管理器(例如npm,bower,ruby gem,ruby bundler)的软件包的node-sass路径。 可以将包含路径的生成数组传递给node-sass。 允许在sass变体,ruby sass,libsass和sass眼镜...

    hyalite:Ruby在Opal中实现的虚拟DOM

    透明质酸 这是使用蛋白石的ruby虚拟DOM实现。 它受react.js的启发。例子require 'hyalite'class ExampleView include Hyalite :: Component state :count , 0 def component_did_mount interval = Proc . new do @...

    monotime:Ruby中单调时间的明智接口

    两者都以纳秒级的分辨率运行,达到您的Ruby实现支持的极限。 例如,要测量经过的时间,请创建一个“ Instant以标记起点,执行操作,然后询问自以下时间以来经过的“ Duration : include Monotime start = ...

    ruby声明式语法的实现例子

    在ActiveRecord可以用很方便的声明方式来定义model之间的关联关系,例如: 代码如下: class Topic < ActiveRecord::Base  has_many :posts  belongs_to :user end has_many和belongs_to其实是Topic类的class ...

    dm-validations:用于对 DM 模型和纯 Ruby 对象执行验证的库

    有两种主要方法可以为您的模型实现验证 1)将带有属性的验证方法作为您的类中的参数 require 'dm-core' require 'dm-validations' class ProgrammingLanguage include DataMapper :: Resource property :name , ...

    nihilist:Nigilist Ruby gem —应用程序中用户模型的空对象

    Nigilist 对于实现空对象模式非常有用。 它允许 null 对象为所有问题方法返回false ,为所有非 bang 方法返回nil为所有复数形式的方法(以及在 Ruby on Rails 中为同名 ActiveRecord 模型提供空关系)返回[] ,而...

    eventful:因为Ruby的Observable永远做不到我想要的

    Eventful是Ruby的Observable模块之上的一个小扩展,它实现了命名事件,块侦听器和事件冒泡。 它提供比Observable通常允许的更加灵活的事件处理行为,后者通常要求侦听器是实现update对象,并且没有提供基于事件类型...

    system_navigation:一个为Ruby程序提供其他自省功能的库

    SystemNavigation是一个Ruby库,它为Ruby程序提供附加的自省功能。 该库定义了许多允许查询的有用方法: 方法,例如,类或全局变量 文字(例如数字,字符串,符号等)的方法。 查找特定字符串的方法(方法源搜索)...

    介绍Ruby中的模块与混合类型的相关知识

    模块实现混合工厂。 模块定义了一个命名空间,一个沙箱中方法和常量可以自由使用,而不必担心踩到其他的方法和常数。 语法: module Identifier statement1 statement2 ........... end 就像被命名为类常量模块...

    ruby-comparateur:计算两个 HTML 文档的结构相似度

    这就是为什么您必须创建一个类并使用Comparateur include或extend它并根据需要使用它。 此实现还允许您构建自己的缓存系统。 安装 将此行添加到应用程序的 Gemfile 中: gem 'comparateur' 然后执行: $ bundle...

    简单的全文搜索Mongoid ORM-Ruby开发

    Mongoid搜索Mongoid搜索是Mongoid ORM的一种...在您的Gemfile中安装:gem'mongoid_search'然后:bundle install示例类产品include Mongoid :: Document include Mongoid :: Search字段:brand字段:name字段:unit fie

    distance_of_time_in_words:用Rails更好的时间间隔

    该dotiw库可将distance_of_time_in_words添加到任何Ruby项目中,或使用更准确的输出覆盖Rails中的默认实现。 您是否想将精度降低到秒? 我也是。这就是为什么我制造了这种宝石。 安装 添加到您的Gemfile 。 gem '...

    transitions:从ActiveModel中提取状态机

    transitions是Ruby状态机的实现。 安装 滑轨 这进入您的Gemfile: gem "transitions" , :require => [ "transitions" , "active_model/transitions" ] …并将其纳入您的ORM模型: include ActiveModel :: ...

    linux下GCC编译C程序

    GNU编译器集(其前身为GNU C编译器)诞生于1987年。...日益流行的脚本语言Perl、Python和Ruby,以及正在不断发展的mono 可移植C#实现的确有助于冲淡人们对Linux编程的传统看法,但这完全是另外一个问题了。

    garner:一套实现各种缓存策略的Rack中间件和缓存助手

    加纳 Garner是Ruby和Rack应用程序的缓存层,支持模型和实例绑定以及层次... 接下来,在您的应用程序中包含适当的mixin: 对于普通的Ruby应用程序,请include Garner::Cache::Context 。 对于Rack应用,首先require "g

    CodeIgniter:php敏捷开发框架web快速开发详解

    另一方面,如果你已经花了很大功夫学习 PHP 的话,那么从 Ruby 重新开始又要重头学起。 为 PHP 开发的框架有很多个(大约 40 个),CI 只是其中之一。其它的还包括 Zend Framework、Cake、Trax 等。下列网址可以...

Global site tag (gtag.js) - Google Analytics