• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

rubyBlocksCanBeClosures(摘自programmingruby)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
Let’s get back to our jukebox for a moment (remember the jukebox?). At some point
we’ll be working on the code that handles the user interface—the buttons that people
press to select songs and control the jukebox. We’ll need to associate actions with
those buttons: press START and the music starts. It turns out that Ruby’s blocks are
a convenient way to do this. Let’s start by assuming that the people who made the
hardware implemented a Ruby extension that gives us a basic button class. (We talk
about extending Ruby beginning on page 261.)
1start_button = Button.new("Start")
2pause_button = Button.new("Pause")
3
4

What happens when the user presses one of our buttons? In the Button class, the hardware
folks rigged things so that a callback method, button_pressed, will be invoked.
The obvious way of adding functionality to these buttons is to create subclasses of
Button and have each subclass implement its own button_pressed method.
 1class StartButton < Button
 2def initialize
 3super("Start") # invoke Button's initialize
 4end
 5def button_pressed
 6do start actions
 7end
 8end
 9start_button = StartButton.new
10

This has two problems. First, this will lead to a large number of subclasses. If the
interface to Button changes, this could involve us in a lot of maintenance. Second, the
actions performed when a button is pressed are expressed at the wrong level; they are
not a feature of the button but are a feature of the jukebox that uses the buttons.We can
fix both of these problems using blocks.
 1songlist = SongList.new
 2class JukeboxButton < Button
 3def initialize(label, &action)
 4super(label)
 5@action = action
 6end
 7def button_pressed
 8@action.call(self)
 9end
10end
11

The key to all this is the second parameter to JukeboxButton#initialize. If the last
parameter in a method definition is prefixed with an ampersand (such as &action),
Ruby looks for a code block whenever that method is called. That code block is converted
to an object of class Proc and assigned to the parameter. You can then treat
the parameter as any other variable. In our example, we assigned it to the instance
variable @action. When the callback method button_pressed is invoked, we use the
Proc#call method on that object to invoke the block.

So what exactly do we have when we create a Proc object? The interesting thing is that
it’s more than just a chunk of code. Associated with a block (and hence a Proc object)
is all the context in which the block was defined: the value of self and the methods,
variables, and constants in scope. Part of the magic of Ruby is that the block can still
use all this original scope information even if the environment in which it was defined
would otherwise have disappeared. In other languages, this facility is called a closure.

Let’s look at a contrived example. This example uses the method lambda, which converts
a block to a Proc object.

1def n_times(thing)
2

The method n_times returns a Proc object that references the method’s parameter,
thing. Even though that parameter is out of scope by the time the block is called, the
parameter remains accessible to the block.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ruby快速入门(转)发布时间:2022-07-14
下一篇:
Windows环境下Ruby离线安装gem包发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap