Ask for some experience


#1

hello everyone.
any one like to share some experience about ruby string
just like java: trim(), split(), or indexOf()

all regards


#2

joe-black wrote:

hello everyone.
any one like to share some experience about ruby string
just like java: trim(), split(), or indexOf()

Java split() => Ruby String.split:

“hello world”.split( " ")

returns [ “hello”, “world” ].

Java trim() => Ruby String.strip:

" hello world ".strip

returns “hello world”.

Java indexOf() => Ruby String.index

“hello world”.index( “w”)

returns 6.


#3

that’s cool.
any more? the frequently used.


#4

On Wed, Nov 16, 2005 at 01:09:17AM +0900, Joe B. wrote:

that’s cool.
any more? the frequently used.

irb(main):001:0> “foo”.upcase
=> “FOO”
irb(main):002:0> “foo”.sub(/[aeiou]/, ‘a’)
=> “fao”
irb(main):003:0> “foo”.gsub(/[aeiou]/, ‘a’)
=> “faa”
irb(main):004:0> “FOO”.downcase
=> “foo”
irb(main):005:0> “foo”.capitalize
=> “Foo”
irb(main):006:0> “foo”.tr(“aeiou”, “QWRTY”)
=> “fTT”
irb(main):007:0> “foo”.rjust(10)
=> " foo"
irb(main):008:0> String.help

The last one only works if you have ihelp installed. If not, install it
or do

“foo”.methods.sort


#5

On 15/11/05, Joe B. removed_email_address@domain.invalid wrote:

hello everyone.
any one like to share some experience about ruby string
just like java: trim(), split(), or indexOf()

all regards

Reading the pickaxe and
$ ri String
would be a good idea.

One neat thing I onlz found a few month ago is this
irb(main):001:0> “my string with 28 characters”[/\d+/]
=> “28”
irb(main):002:0> s = “my string with 28 characters”
=> “my string with 28 characters”
irb(main):003:0> bschroed@black:~/svn$ irb1.8
irb(main):001:0> s = “my string with 28 characters”
=> “my string with 28 characters”
irb(main):002:0> s[/\d+/]
=> “28”
irb(main):003:0> s[/\d+/] = “twentyeight”
=> “twentyeight”
irb(main):004:0> s
=> “my string with twentyeight characters”
irb(main):005:0> s[/string (\w+) (\w+)/]
=> “string with twentyeight”
irb(main):006:0> s[/string (\w+) (\w+)/, 1]
=> “with”
irb(main):007:0> s[/string (\w+) (\w+)/, 2]
=> “twentyeight”

Especially line three is really cool.

cheers,

Brian


http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/