`
liss
  • 浏览: 826306 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

ruby学习笔记(11)--基本的输入输出

    博客分类:
  • Ruby
阅读更多

#比较让人郁闷的是gets在我的机器上都不能使用
#所以无法得到控制台输入,先从文件io开始了
file=File.open('io.rb')
puts file.gets #file=File.open('io.rb')
line=file.gets
#line.each_byte{|ch| putc ch} #puts file.gets #file=File.open('io.rb')
#file.each_line{|line| puts line} #line 3..end
file.close
#IO.foreach只读,以行为单位
#IO.foreach('io.rb'){|line| puts line}
#IO.read 一次性全部读取
str= IO.read('io.rb')
#IO.readlines 逐行读取
arr= IO.readlines('io.rb')
#stringio
require 'stringio'
strin=StringIO.new("hello \n world",'r')
strout=StringIO.new("",'w')
strin.each_line{|line| strout.puts line.reverse}
puts strout.string #olleh dlrow
#newwork
require 'net/http'
http=Net::HTTP.new("www.sina.com.cn",80)
response=http.get('/',nil)
puts response.body.length
#这个open-uri会报404 Not Found错误
require 'open-uri'
open("http://www.sina.com.cn/") {|f|
puts f.read.length
}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2006/12/04/1429270.aspx

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics