require 'stringio'
str = StringIO.new %{This is a test of a string as a file. \r\n
And this could be another line in the file}
# Get a line
str.gets # => "This is a test of a string as a file. \r\n"
# Get the next 18 charaters
str.read(18) # => " And this could be"
# Seek to new position and read 7 more characters
str.pos = 59 # => 59
str.read(7) # => "another"
# Rewind the file and overwrite some of the existing text
str.rewind
str.write("Here's how to use")
# Rewind again and output the new contents
str.rewind
str.read # => "Here's how to use a string as a file.
\r\n And this could be another line in the file"
str.eof? # => true
The "Ruby Pocket Reference" by Michael Fitzgerald is a concise and practical guide designed to serve as a quick reference for Ruby programmers. Published in its first edition in July 2007 by O'Reilly ...
相关推荐
The "Ruby Pocket Reference" by Michael Fitzgerald is a concise and practical guide designed to serve as a quick reference for Ruby programmers. Published in its first edition in July 2007 by O'Reilly ...
- **String类**: Ruby使用String类的对象表示文本。 - **操作**: 提供了丰富的操作方法,如提取子字符串、插入和删除文本、搜索及替换等。 7. **数组** - **定义**: 数组是一系列值的集合,可以通过索引来访问。...
<li><a href="/ruby">Ruby</a> ''' handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element ...
除了上述方法,还可以使用其他语言(如Java、Ruby等)的库或自定义解析逻辑来读取`.ini`文件。关键在于理解文件结构,并使用适当的字符串处理或解析工具。 6. 安全性和权限: 在实际应用中,读取配置文件时要注意...
using System.IO; string currentDir = Directory.GetCurrentDirectory(); Console.WriteLine(currentDir); ``` 5. **Ruby**: Ruby的`Dir`类提供了`pwd`方法来获取当前目录: ```ruby Dir.pwd ``` 6. **...
string scriptContent = File.ReadAllText("myScript.vbs"); scriptEngine.AddCode(scriptContent); ``` 现在,我们已经可以调用脚本中的函数了。例如,我们可以调用上面定义的`AddNumbers`函数: ```csharp int ...
Hadoop Streaming是一个接口,允许使用任何可执行文件或脚本(如C、C++、Python、Ruby等)作为Mapper和Reducer。它通过Unix标准输入输出进行数据交换。每个Key/Value对以一个tab分隔,标准输入作为Mapper的输入,...
foreach (string line in Iterator.ReadLines(@"your file name")) { Console.WriteLine(line); } ``` 这段代码会逐行打印文件的内容。 2. 使用迭代器和谓词筛选文件中的行 除了简单地遍历文件行,还可以结合...
gbk_content = read(gbk_file, String) utf8_content = convert_encoding(gbk_content, "GBK", "UTF-8") write(utf8_file, utf8_content) close(gbk_file) close(utf8_file) ``` 除了基础的转换功能,`...