关键字和预定义变量:__END__和DATA的问题

两个文件,a.rb和b.rb
当a.rb中有__END__表示的注释,并且需要通过DATA关键字来获得的时候,
如果直接执行a.rb,也就是当$0和__FILE__为同一文件对象时,
DATA可以读取到a.rb中__END__后面的内容.

如果a.rb被b.rb调用执行,
比如b.rb中load "a.rb",
这样a.rb中的DATA其实是b.rb中的对象,所以DATA只能得到b.rb中的__END__之后的内容.


一直很期待能有个解决方法让a.rb不论如何执行都可以得到本身的DATA对象,
但问了核心开发组,结果Ruby他爸Matz说没办法.
那我只能把本来写在__END__后面的内容单独写到一个模板文件中,然后然a.rb读取这个文件对象当作DATA对象了.


[quote]Charles Cui 发送至 ruby-core
显示详细信息 15:33 (18 小时前)
how to get global constant DATA in file <a.rb>,if a.rb is loaded by b.rb.
when b.rb is exec by Ruby,even if the DATA is used in a.rb,infact the DATA is from b.rb,
because a.rb is loaded by b.rb.

how to resolve it?

such as:

<b.rb> :

Cui$ cat /usr/local/ruby-1.8.7-p160/bin/roadrunner
#!/usr/local/ruby-1.8.7-p160/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'roadrunner' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0"

if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end

gem 'roadrunner', version
load Gem.bin_path('roadrunner', 'roadrunner', version)

<a.rb>
#this is code

__END__
#!/usr/bin/env ruby
#main.rb
#this is the DATA
回复
转发
回复所有人
Charles Cui 发送至 ruby-core
显示详细信息 15:33 (18 小时前)
- 显示引用文字 -
回复
转发
回复所有人
[color=red]Yukihiro Matsumoto 发送至 ruby-core
显示详细信息 15:56 (18 小时前)
Hi,

In message "Re: [ruby-core:30175] [Problem] DATA and __END__ in a loaded rb file"
on Wed, 12 May 2010 16:33:57 +0900, Charles Cui <zheng.cuizh@gmail.com> writes:

|how to get global constant DATA in file <a.rb>,if a.rb is loaded by b.rb.
|when b.rb is exec by Ruby,even if the DATA is used in a.rb,infact the DATA
|is from b.rb,
|because a.rb is loaded by b.rb.

DATA is only available from the script file.

matz.[/color]

回复
转发
回复所有人
Urabe Shyouhei 发送至 ruby-core
显示详细信息 17:29 (16 小时前)
(2010/05/12 16:56), Yukihiro Matsumoto wrote:
> DATA is only available from the script file.

True, but I know the reporter's feeling. Ruby stops parsing at __END__ even on
a library file so it's natural for a programmer to expect DATA be file local.
Can there be any future addition of DATA[__FILE__] or something like that?

回复
转发
回复所有人
Magnus Holm 发送至 ruby-core
显示详细信息 17:35 (16 小时前)
Right now you'll have to do something like:

data = File.read(__FILE__) =~ /^__END__\n/ && $' || ''

// Magnus Holm
- 显示引用文字 -[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,遍历unordered_set容器有多种方法。下面是三种常见的方法: 方法一:使用迭代器进行遍历 可以使用迭代器来遍历unordered_set容器中的元素。例如,可以使用auto关键字来定一个迭代器,并用begin()和end()函数获取unordered_set容器的起始和结束迭代器。然后,使用循环将迭代器逐个移动并输出元素。 示例代码如下: ``` std::unordered_set<std::string> uset{ "http://c.biancheng.net/c/", "http://c.biancheng.net/java/", "http://c.biancheng.net/linux/" }; for (auto iter = uset.begin(); iter != uset.end(); iter++) { std::cout << *iter << std::endl; } ``` 方法二:使用范围for循环遍历 使用C++11引入的范围for循环语法,可以更简洁地遍历unordered_set容器中的元素。在每次循环中,元素被自动赋值给循环变量,并可以直接输出。 示例代码如下: ``` std::unordered_set<std::string> uset{ "http://c.biancheng.net/c/", "http://c.biancheng.net/java/", "http://c.biancheng.net/linux/" }; for (const auto& element : uset) { std::cout << element << std::endl; } ``` 方法三:使用算法库函数进行遍历 可以使用算法库函数std::for_each来遍历unordered_set容器中的元素。该函数接受一个函数对象(或Lambda表达式)和容器的起始和结束迭代器,并将函数对象应用于每个元素。 示例代码如下: ``` #include <algorithm> void printElement(const std::string& element) { std::cout << element << std::endl; } std::unordered_set<std::string> uset{ "http://c.biancheng.net/c/", "http://c.biancheng.net/java/", "http://c.biancheng.net/linux/" }; std::for_each(uset.begin(), uset.end(), printElement); ``` 这样就可以使用上述三种方法之一来遍历unordered_set容器中的元素。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [STL无序容器之unordered_set和unordered_multiset](https://blog.csdn.net/andyjkt/article/details/116495385)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值