`
ericluo
  • 浏览: 17705 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Fwd: Re: What does this construct mean?

阅读更多

-------------------- Start of forwarded message --------------------
From: Devin Mullins <twifkak@comcast.net>
Newsgroups: comp.lang.ruby
Subject: Re: What does this construct mean?
Date: Sat, 16 Jul 2005 10:57:37 +0900

Casper wrote:

>1. class MyController < ActionController::Base
>2. helper :my_helper
>3. end
>
>I see this construct in Ruby on Rails a lot, and I don't know what is
>going on in line 2.
>
>Is this invoking a method called "helper" with the argument
>":my_helper"? And if so, when does this get invoked?
>
>Thanks.
>
>
You are on the money. I will attempt to explain, but you should read the
PickAxe for the full Ruby goodness.

class sdgksjdgk
..
end

Is just a way of scoping your code. In particular, it redefines self, it
creates a new lexical scope (that is, a new binding for local
variables), it tells class variables (@@foo) where to hook up to, and it
tells method definitions where to hook up to. Run `ruby -e"p self" ` and
you'll discover that you're always inside a class...end block.

So
class Blob
puts "Hello!"
end

Prints "Hello!" How does it do that? Well, you're trying to call "puts"
which really means you're trying to call "self.puts". What's self? Well,
let's find out:

a = class Blob
self #like defs and blocks, class returns the last thing evaluated
end
=> Blob
a.class
=> Class

Self is the Class object named Blob. So when you type "puts" inside a
class block, you're calling the instance method Blob.puts. Where does
it get puts from? Well, Blob is a Class, and Class inherits Object, and
Object includes Kernel, and Kernel has a puts method defined on it.

When you write
class Blob
def murh
puts "Hello!"
end
end
you're referring to an entirely different self.puts, of course. When
"murh" actually gets invoked, self will be the Blob instance it gets
invoked on, instead of the Class instance named Blob. But when the
method is actually defined, Ruby knows to define it as an instance
method of whatever class...end block it's inside.

So, when you call helper :my_helper, bingo, you're calling a method.
Specifically, Blob.helper. Not Blob#helper, which is the instance
method, where self would be some instance of Blob -- rather this
something akin to Class#helper. (When I say "something akin to" I'm
referring to singleton classes. That's a whole 'nother email, though.)

Where is it getting helper from? Well, in short, it's getting it from
ActionController::Helpers::ClassMethods in action_controller/helpers.rb.
The long version involves explaining what singleton classes are.

Hope that helps.

Sidenote - Adanced Rubyage:

Earlier I said that class...end does more than redefine self. This is
important. Notice that, of the following two, the first works but the
second doesn't:

#1:
class Moo
@@fun = 0
def Moo.fun
@@fun += 1
puts @@fun
end
end

#2:
class Moo
@@fun
end
def Moo.fun
@@fun += 1
puts @@fun
end

Here's another example where the scope of a given thing is not dependent
on self, but dependent on what class...end block I'm inside.

class Borg
def blong
def frooz
puts 'Hi!'
end
end
end
a = Borg.new; b = Borg.new
a.frooz #=> Error
a.blong
a.frooz #=> Hi!
b.frooz #=> Hi!

If the 'def' keyword were dependent upon self, then the above would not
work. Notice that, when 'def blong' is run, self is the Class object
named Borg. When 'def frooz' is run, self is the Borg object assigned to a.

Rather, a 'def' keyword is tied to what class it's in. See the following
wacky example:

class A
end

class B
def A.moo
def moo
puts 'mooo'
end
end
end

a = A.new
b = B.new

A.moo

a.moo #=> Error
b.moo #=> mooo

But if you find a legitimate reason to do like that, please let me
know. I'm quite curious.

BTW, if you read a couple of threads back, class...end isn't the only
way to establish such a context. There's also Class.new and
Module#class_eval -- running 'def' inside those is like running it
inside a proper class...end block.

Oh boy. I'm feeling kinda light-headed now...

Devin




-------------------- End of forwarded message --------------------

分享到:
评论

相关推荐

    Fwd: Extension-crx插件

    Fwd:文本,Fwd:链接,Fwd:图像,Fwd:捕获-保存您访问Fwd:Wiki的任何网站中的数据。 Google Meet出勤报告(具有自动允许/无依赖性),您可以将Meets组织到组中-添加| 编辑 删除以HTML文件格式下载报告或推送至...

    fwd:FWD-Web应用程序的便捷环境

    烟花网注意- fwd是死的,万岁 FWD项目已中止,转而使用我们的新解决方案 。 FWD为开发团队利用Docker容器进行本地开发提供了很多帮助。 kool现在以更大的功能和灵活性继续执行相同的任务,它的目标是使个人和团队...

    fwd::locomotive:可能的小货代

    fwd-可能的小货代 关于 fwd是用golang编写的网络端口转发器。 它是跨平台的,支持多种架构,使用起来非常简单。 阅读所有关于它这。 用例 前轮驱动 :heart_suit_selector: 恩格罗克 我必须承认ngrok是fwd的巨大...

    FwdPortForwardingApp:适用于Android的端口转发应用

    FWD:端口转发应用 Fwd:端口转发应用程序可让您使用Android设备的网络接口通过栓系连接转发端口,非常适合托管服务器或打开NAT类型进行游戏。 Fwd旨在促进从Android设备轻松便捷地进行端口转发。 对于对端口转发感...

    fwd:一个非常简单的 JavaScript 插件,可帮助管理浏览器中特定于页面的 JavaScript 代码

    转发 一个非常简单的 JavaScript 插件,用于... var fwd = new Fwd(); fwd.links({ routes: { "about": "about", "services/:id": "showService", "services": "services", "contact": "contact" }, abou

    msr-fwd:磁条卡读卡器 HTTP 数据转发器

    配置可以在/usr/local/etc/msr-fwd.cfg找到配置。安装如果要构建包,请确保已安装 。 Debian 和衍生产品: git submodule update --init./package.shdpkg -i python-msr-cli_0.0.1-1_all.debdpkg -i msr-fwd_0.0.1-1...

    Scara_fwd:Scara 转发机制-matlab开发

    在这个项目中,“Scara_fwd”可能指的是前向运动学(Forward Kinematics)的实现,这是机器人学的一个关键部分。前向运动学涉及将机器人的关节参数(如角度)转换为末端执行器(如工具或抓手)在工作空间中的位置和...

    FWD:Web开发高级跟踪Javascript项目

    目录 简介-项目目标 ... 技术领域 ... CSS3 JavaScript版本:ES2015 / ES6 发射 ... 单击导航栏链接时,可以平滑滚动到各个部分。... 当部分在视口中时,将活动状态添加到相关的导航项中。... 滚动到页面顶部的按钮,只有当用户...

    cpp-std-fwd:C ++ std标头的前向声明

    cpp-std-fwd 转发C ++ 17标准库中最有用的运行时类的声明。 免责声明:该项目的目的是为标准化std的前向声明标头的提议提供概念证明。 使用UB是UB,只能用来评估提案(请参阅底部的常见问题解答)。 基准测试 TL; ...

    FwdPortForwardingApp:适用于Android的端口转发应用 开发技术.zip

    《FwdPortForwardingApp:Android端口转发应用开发技术详解》 在移动设备的使用过程中,有时我们需要在Android平台上实现端口转发功能,以便于远程访问设备上的服务或者调试本地网络应用。FwdPortForwardingApp就是...

    weather-journal-app-fwd:Weather Journal App-第二个项目-Udacity-FWD-专业网站开发-NanoDegree

    Weather Journal App-第二个项目-Udacity-FWD-专业网站开发-NanoDegree Github上的项目链接: : 使用Web API和用户数据来动态更新Weather-Journal App的UI的异步Web应用程序。 此应用根据用户输入的城市名称从 API...

    FWD FWD FWD-crx插件

    将社交媒体分享到正确的环境中。 当此扩展名侦听来自Facebook上经常共享的网站的链接时,该链接的标题将以FWD:FWD:FWD:为前缀。 支持语言:English

    Investigation_data_analysis_FWD:这是我为数据分析领域的 Udacity 纳米学位课程所做的一个项目..我希望这能有所帮助

    (共享单车数据探索) by (哈南阿里) 数据集 我的数据是 2019 年来自(福特 GoBike)系统的大约 2000000 条记录,有 16 个功能..我从这个网站“ ”下载了它 我选择了 2019 年第一季度和第三季度的数据 .....

    dpdk18版本下l2fwd自定义修改目的mac地址

    然后放入l2fwd文件夹中。 编译如下: # export RTE_SDK=/root/dpdk # export RTE_TARGET=build # make CC main.o LD l2fwd INSTALL-APP l2fwd INSTALL-MAP l2fwd.map 适配dpdk-17.11.1,命令通过新增-d指定...

    dpdk17版本下的l2fwd支持目的mac地址手动修改

    LD l2fwd INSTALL-APP l2fwd INSTALL-MAP l2fwd.map 适配dpdk-17.11.1,命令通过新增-d指定端口的dmac。 ./build/l2fwd -c 3 -n 2 -- -p 3 -d 11:11:11:11:11:11 -d 22:22:22:22:22:22 Port0的dmac为: 11:11:...

    Single Phase Controlled Full-wave rectifier with FWD:Single Phase Controlled Full-wave rectifier with Free Wheeling diode-matlab开发

    单相可控全波整流器是一种常见的电力电子电路,它用于将交流电转换为直流电。在该系统中,加入自由轮换二极管(也称为续流二极管)可以提高电路性能并解决某些问题。MATLAB是数学计算、建模和仿真的一种强大工具,常...

    Fwd_ 计算机英语词汇Fwd_ 计算机英语词汇

    Fwd_ 计算机英语词汇Fwd_ 计算机英语词汇

    fwd.zip_NOISE_fwd

    "fwd.zip_NOISE_fwd" 提供的功能文件是专门用于生成图像噪声的工具。这个功能可能对研究图像噪声特性、测试去噪算法或理解噪声对图像质量影响的研究人员非常有用。 噪声通常指的是图像中不期望存在的随机变化,可以...

    DPDKL2fwd代码走读报告(代码流程分析).pdf

    DPDKL2fwd代码走读报告(代码流程分析) DPDK(Data Plane Development Kit)是一种高性能的网络数据包处理平台,能够在通用x86服务器上实现高速网络数据包转发。该平台通过使用hugepage、uio、zero copy、cpu ...

Global site tag (gtag.js) - Google Analytics