转载——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
相关文档:
在使用中ruby的过程中难免会遇到提高性能的问题,由此便想起了ruby线程。但是我在使用中却发现ruby的线程却不能提高性能。我写了以下代码,做了些简单测试。
代码
# -*- coding: GB2312 -*-
require 'date'
# 使用线程,线程的处理代码里没有sleep
def have_thread_no_sleep
p Time.now
thread1 = Thread.new do
......
原文连接: 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 (命令行输入后, ......
env setup
linux(ubuntu)下ruby开发环境搭建,包括一些常见问题解决
注意,本文只是我在搭建ruby学习环境时的一些笔记,因为是用gedit编辑的,所以格式化不是很好,另外,只是备忘而已。
2010.1.19
1. install ruby
$ tar xzf ruby-1.8.7-p248.tar.gz
$ mv ruby-1.8.7-p248 ruby187
$ cd ruby187/
$ ./configure
......
文章转自 http://www.ej38.com/showinfo/Ruby-140367.html
过程如下:
1、ruby下载一键安装:
http://rubyforge.org/frs/download.php/29263/ruby186-26.exe
ruby -v 显示版本,安装成功
2、下载rubygems安装:
http://rubyforge.org/frs/download.php/60719/rubygems-1.3.5.zip
解压,ruby setup.rb
g ......
#一、数组引用
arr=[3,4,5,6,7,8,9]
puts arr[0] #3
puts arr.first #3
puts arr[arr.length-1] #9
puts arr[arr.size-1] #9
puts arr.last #9
puts arr[-1] #9
puts arr[-2] #8
print arr[1..3] ,"\n" #456
print arr[-3,4] ,"\n" #789,从-3开始 ,打印4个元素,这里只有三个
#Ruby的数组大小是动态的,你能够 ......