`
Hooopo
  • 浏览: 341574 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Using a String as a File in Ruby

    博客分类:
  • Ruby
阅读更多
编辑器不能用啦。。。
分享到:
评论
3 楼 Hooopo 2009-08-24  
irb(main):001:0> require 'stringio'
=> true
irb(main):002:0> s = StringIO.new
=> #<StringIO:0x2ced9a0>
irb(main):003:0> s << 'foo'
=> #<StringIO:0x2ced9a0>
irb(main):004:0> s << 'bar'
=> #<StringIO:0x2ced9a0>
irb(main):005:0> s.string
=> "foobar"
2 楼 Hooopo 2009-08-24  
>> s = StringIO.new
=> #<StringIO:0x3659d4>
>> s << 'foo'
=> #<StringIO:0x3659d4>
>> s << 'bar'
=> #<StringIO:0x3659d4>
>> s.pos
=> 6
>> s.rewind
=> 0
>> s.read
=> "foobar"
1 楼 Hooopo 2009-08-24  
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

相关推荐

    Ruby Pocket Reference

    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 ...

    ruby语言基础教程.pptx(共17页,携程内部培训版)

    - **String类**: Ruby使用String类的对象表示文本。 - **操作**: 提供了丰富的操作方法,如提取子字符串、插入和删除文本、搜索及替换等。 7. **数组** - **定义**: 数组是一系列值的集合,可以通过索引来访问。...

    Python api 库函数学习

    &lt;li&gt;&lt;a href="/ruby"&gt;Ruby&lt;/a&gt; ''' handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element ...

    Linux编程读取ini文件

    除了上述方法,还可以使用其他语言(如Java、Ruby等)的库或自定义解析逻辑来读取`.ini`文件。关键在于理解文件结构,并使用适当的字符串处理或解析工具。 6. 安全性和权限: 在实际应用中,读取配置文件时要注意...

    获取当前文件夹的位置的资源

    using System.IO; string currentDir = Directory.GetCurrentDirectory(); Console.WriteLine(currentDir); ``` 5. **Ruby**: Ruby的`Dir`类提供了`pwd`方法来获取当前目录: ```ruby Dir.pwd ``` 6. **...

    C#利用MSScriptControl调用脚本实例

    string scriptContent = File.ReadAllText("myScript.vbs"); scriptEngine.AddCode(scriptContent); ``` 现在,我们已经可以调用脚本中的函数了。例如,我们可以调用上面定义的`AddNumbers`函数: ```csharp int ...

    在Hadoop的MapReduce任务中使用C程序的三种方法

    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. 使用迭代器和谓词筛选文件中的行 除了简单地遍历文件行,还可以结合...

    StringEncodings.jl:使用iconv在Julia中进行字符串编码转换

    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) ``` 除了基础的转换功能,`...

Global site tag (gtag.js) - Google Analytics