Ruby string processing

Source: Internet
Author: User

Ruby processes strings like numbers. We enclose them with single quotation marks ('...') or double quotation marks.

Ruby> "abc"
"Abc"
Ruby> 'abc'
"Abc"


Single quotation marks and double quotation marks have different functions in some cases. A string enclosed by double quotation marks allows a character to be led out by a front slash, and can be embedded with a #{} expression.

Strings enclosed in single quotes do not explain the strings. What are you looking at? Several examples:

Ruby> print "a \ nb \ nc", "\ n"
A

C
Nil
Ruby> print 'a \ nb \ n', "\ n"
A \ nb \ nc
Nil
Ruby> "\ n"
"\ N"
Ruby> '\ N'
"\ N"
Ruby> "\ 001"
"\ 001"
Ruby> '\ 001'
"\ 001"
Ruby> "abcd # {5*3} efg"
"Abcd 15 efg"
Ruby> var = "abc"
"Abc"
Ruby> "1234 # {var} 5678"
"1234 abc 5678"


Ruby's string operations are more agile and intuitive than C. For example, you can use + to concatenate several strings and use * to repeat a string several times:

Ruby> "foo" + "bar"
"Foobar"
Ruby> "foo" * 2
"Foofoo"


In contrast, in C, because precise memory management is required, concatenating strings is much more clumsy:

Char * s = malloc (strlen (s1) + strlen (s2) + 1 );
Strcpy (s, s1 );
Strcat (s, s2 );
/*...*/
Free (s );


But for Ruby, we do not need to consider the space occupation of strings, which frees us from the cumbersome memory management.

Below are some strings for processing,

Series:

Ruby> word = "fo" + "o"
"Foo"


Repeat:

Ruby> word = word * 2
"Foofoo"


Extract characters (Note: In Ruby, the characters are considered as integers ):

Ruby> word [0]
102 #102 is ASCII code of 'F'
Ruby> word [-1]
111 #111 is ASCII code of 'O'


(A negative index refers to the offset calculated from the end of the string, rather than from the string header .)

Extract substrings:

Ruby> herb = "parsley"
"Parsley"
Ruby> herb [0, 1]
"P"
Ruby> herb [-2, 2]
"Ey"
Ruby> herb [0 .. 3]
"Pars"
Ruby> herb [-5...-2]
"Rsle"


Check equal:

Ruby> "foo" = "foo"
True
Ruby> "foo" = "bar"
False


Note: In Ruby 1.0, the above results are displayed with uppercase letters.

Well, let's try these features. The following is a guessing puzzle. Maybe the word "Puzzle" is a little cool in the following ;-)

# Save this as guess. rb
Words = ['foobar', 'baz', 'quux ']
Secret = words [rand (3)]
Print "guess? "
While guess = STDIN. gets
Guess. chop!
If guess = secret
Print "You win! \ N"
Break
Else
Print "Sorry, you lose. \ n"
End
Print "guess? "
End
Print "The word was", secret, ". \ n"


Now, don't worry too much about the code details. The following is a dialogue for running the puzzle program.

% Ruby guess. rb
Guess? Foobar
Sorry, you lose.
Guess? Quux
Sorry, you lose.
Guess? ^ D
The word was baz.


(Considering the success rate of 1/3, maybe I should have done a little better .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.