易截截图软件、单文件、免安装、纯绿色、仅160KB

转载——Ruby字符串处理

转自:http://developer.51cto.com/art/200912/170762.htm 
Ruby字符串处理函数总结列表分享
Ruby字符串处理函数包括返回字符串长度函数;判断字符串中是否包含另一个串函数;字符串插入;字符串分隔,默认分隔符为空格等等。
 
str.length => integer 
str.include? other_str
 => true or false  
"hello".include? "lo"
 #=> true  
"hello".include? "ol"
 #=> false  
"hello".include? ?h
 #=> true 
Ruby语言对于编程人员来说是一项非常有用的函数。学好这项语言可以帮助我们实现简便轻松的编写方式。下面我们向大家介绍一下Ruby字符串处理函数的一些基本概念。
Ruby字符串处理函数1.返回字符串的长度
Ruby字符串处理函数2.判断字符串中是否包含另一个串
Ruby字符串处理函数3.字符串插入:
str.insert(index, other_str) 
=> str   "abcd".insert(0, 'X')
 #=> "Xabcd"  "abcd".insert(3, 'X')
 #=> "abcXd"  "abcd".insert(4, 'X') 
#=> "abcdX"  "abcd".insert(-3, 'X')    -3, 'X') #=> "abXcd"  "abcd".insert(-1, 'X')
 #=> "abcdX" 
Ruby字符串处理函数4.字符串分隔,默认分隔符为空格
str.split(pattern=$;, [limit])
 => anArray   " now's the time".split #=> 
["now's", "the", "time"]  "1, 2.34,56, 7".split(%r{,\s*})
 #=> ["1", "2.34", "56", "7"]  "hello".split(//) #=> 
["h", "e", "l", "l", "o"]  "hello".split(//, 3) #=> 
["h", "e", "llo"]  "hi mom".split(%r{\s*}) #=> 
["h", "i", "m", "o", "m"]   "mellow yellow".split("ello")
 #=> ["m", "w y", "w"]  "1,2,,3,4,,".split(',') #=>
 ["1", "2", "", "3", "4"]  "1,2,,3,4,,".spl


相关文档:

rails on ruby exmaples depot

1.install ruby
    download from http://www.ruby-lang.org/en/news/2009/12/07/ruby-1-9-1-p376-is-released/
2.install rails
   ruby gem install rails --include-dependencies
3.install mysql
   download from http://sourceforge.net
   ruby gem install&nb ......

交互式 Ruby Shell irb

irb 是从命令行运行的
irb 的命令行选项(摘自 Porgramming Ruby 第二版)
-f
禁止读取~/.irbrc Suppress reading ~/.irbrc.
-m
数学模式(支持分数和矩阵) Math mode (fraction and matrix support is available).
-d
设置#DEBUG为true(同ruby -d一样)  Set $DEBUG to true (same as ``ruby -d'').
-r lo ......

ruby:查看版本,命令行运行程序


原文连接: http://hi.baidu.com/%B7%CF%B2%C5%CB%FB%B8%E7/blog/item/09c19411244152daf7039ec4.html
通过命令行查看ruby版本信息:
ruby -v
命令行运行程序:
方法1.
ruby -e 'print "hello ruby"'
-e 表示将后面的一行作为ruby程序
print 是ruby的一个内置函数
方法2.交互编译环境
irb (命令行输入后, ......

Ruby学习笔记一——语言基础


#一、这里是注释,是单行注释,类似于//
puts 3/5#这里是整数形式的结果
puts 3/5.0#这里是小数形式的结果
=begin
  这是多行注释,实际上这也是Ruby内嵌文档格式,类似于Java doc
  =end不但要有起止,还要缩进才有用。
=end
#二、连行
puts "Hello Ruby!"; puts "This is a "\
"String";# ......

Ruby学习笔记四——模块


#一、模块定义及引用,模块就是一段代码,里面有一些方法放一起。
#定义模块用module...end 。模块与类非常相似,但是:
#A) 模块不可以有实例对象;
#B) 模块不可以有子类。
include Math
puts sqrt(91);
module Me
  def sqrt(a)
  puts a*a;
  return a*a;
end
PI=3.1415926 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号