`
sqlxx
  • 浏览: 17962 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

About class << self in ruby

 
阅读更多

在ruby中经常可以见到这样的写法:

class A
  class << self
     def hello
        puts "hello"
     end
   end
end

这样的写法和

class A
  def self.hello
     puts "hello"
   end
end

可以说是完全一样的。第一种写法的一个好处是,如果需要在一个class或者module里面定义多个类级别的方法, 这种写法可以少写很多个"self." :)

还有一个好处是,可以使用attr_reader/attr_accessor 之类的meta programming的method.



分享到:
评论

相关推荐

    Ruby.Pocket.Reference

    and the description "Ruby.Pocket.Reference.Jul.2007 learning ruby 的 can reference" indicate that this is a concise guide designed to provide quick access to essential information about the Ruby ...

    ruby cookbook

    like how to blink the lights on your keyboard.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;The Ruby Cookbook is the most useful book yet&lt;br&gt;written about Ruby. When you need to solve a problem, don't reinvent&lt;br&gt;the wheel: ...

    Ruby-Way.rar_About Language

    《Ruby Way》是由Hal Fulton编写的关于Ruby编程语言的一本著作。这本书深入浅出地探讨了Ruby语言的各种特性,旨在帮助读者理解并掌握这门强大的动态脚本语言。Ruby以其简洁、优雅的语法和强大的元编程能力而备受赞誉...

    Visual C++ 编程资源大全(英文源码 文件)

    A class for gathering file information recursively through directories(63KB)&lt;END&gt;&lt;br&gt;7,self_extractor.zip&lt;br&gt;A class that allows you to create self extracting executables for use in distribution or ...

    Android虚拟机Dalvik完整源码

    This directory contains the Dalvik virtual machine and core class library, as well as related tools, libraries, and tests. A note about the licenses and header comments ------------------------------...

    ruby-language-quickref.zip_About Language

    在"ruby-language-quickref.zip_About Language"这个压缩包中,我们重点关注的是Ruby语言本身的基础知识和特性。"ruby language quickref.pdf"很可能是一个Ruby语言速查指南,涵盖了语言的关键概念和语法。 首先,...

    Ford's ApproachTo Developing Self-Driving Vehicle

    5 Best in class partners: working with Argo AI 6 How we earn trust-Applying safety processes 7 How we earn trust-Designing for reliability 8 How we earn trust-Delivering a valuable customer experience...

    Ruby-HighVoltage容易在Rails应用程序中包含静态页面

    class PagesController &lt; HighVoltage::Pages # 添加你自己的逻辑,如权限控制、缓存策略等 end ``` ### 高级用法 1. **分页**:尽管High Voltage主要用于单个静态页面,但通过自定义控制器,你可以实现多页内容...

    Pro ActiveRecord Databases

    &lt;br&gt;or maybe as a chapter or two, within a larger scoped book generally about the Ruby on&lt;br&gt;Rails (RoR) framework. And almost all of those books actually do a great job of introducing&lt;br&gt;you to the ...

    About Time AND Timer sample in STEP7_20070903a.zip西门子PLC编程实例程序源码

    About Time AND Timer sample in STEP7_20070903a.zip西门子PLC编程实例程序源码About Time AND Timer sample in STEP7_20070903a.zip西门子PLC编程实例程序源码About Time AND Timer sample in STEP7_20070903a.zip...

    Visualization Handbook PDF part 2 of 2

    The field of visualization is focused on creating&lt;br&gt;images that convey salient information about&lt;br&gt;underlying data and processes. In the past&lt;br&gt;three decades, the field has seen unprecedented&lt;br&gt;...

    rails _sunspot 学习笔记

    &lt;% for row in @search.facet(:search_class).rows %&gt; &lt;li&gt; &lt;% if params[:class].blank? %&gt; &lt;%= row.count %&gt; &lt;%= link_to(row.value, :class =&gt; row.value) %&gt; &lt;% else %&gt; &lt;strong&gt;&lt;%= row.value %&gt;&lt;/strong...

    ruby——course.pdf

    class Matz &lt; Person def initialize super('Yukihiro Matsumoto') end end puts Matz.new.greet # 输出 "Hello, my name is Yukihiro Matsumoto." ``` ### 5. 控制结构 Ruby提供了常见的控制结构,如条件语句...

    H5 jQuery CSS3带时钟幻灯片自动轮播特效.zip

     &lt;li&gt; &lt;a &gt; &lt;i class="fa fa-edit"&gt;&lt;/i&gt; &lt;strong&gt;About us&lt;/strong&gt; &lt;small&gt;sweet home&lt;/small&gt; &lt;/a&gt; &lt;/li&gt;  &lt;li&gt; &lt;a &gt; &lt;i class="fa fa-gift"&gt;&lt;/i&gt; &lt;strong&gt;Features&lt;/strong&gt; &lt;small&gt;sweet home&lt;/small&gt; &lt;/a&gt; ...

    如家宠物网站

    xml=&lt;data&gt;&lt;config&gt;&lt;isShowTitle&gt;0&lt;/isShowTitle&gt;&lt;autoPlayTime&gt;5&lt;/autoPlayTime&gt;&lt;/config&gt;&lt;channel&gt;&lt;item&gt;&lt;link&gt;&lt;/link&gt;&lt;image&gt;images/0411412705084558.jpg&lt;/image&gt;&lt;title&gt;&lt;/title&gt;&lt;/item&gt;&lt;item&gt;&lt;link&gt;&lt;/link&gt;...

    All New Electronics Self Teaching Guide (Self-Teaching Guides) by Harry Kybett

    Three things Boysen's book were unclear about are: ground line in circuits, which doesn't even show up in the index and is assumed to be something the reader knows beforehand; combining AC and DC in ...

    Radio Frequency Integrated Circuit Design

    Wave&lt;br&gt;Transmission Line 379&lt;br&gt;10.9 Class G and H Amplifiers 381&lt;br&gt;10.10 Class S Amplifiers 383&lt;br&gt;&lt;br&gt;xiv Radio Frequency Integrated Circuit Design&lt;br&gt;10.11 Summary of Amplifier Classes for RF ...

Global site tag (gtag.js) - Google Analytics