Substitutions in Ruby

Ruby codes with output:

Here I don’t waste your time to read the theories. Then only i just write the ruby code and as well as output.

Procedure:

* Install ruby 1.8.6 and goto command prompt TYPE irb or use any editors with the extension of .rb

Excercise:

1. irb(main):001:0> puts “foobar”.sub(‘bar’, ‘foo’)

Output: foofoo

2.irb(main):002:0>puts “this is a test”.gsub(‘i’, ”)
Output: ths s a test

3. >x = “This is a test”
>puts x.sub(/^../, ‘Hello’)

Output: Hellois is a test

4.>x = “This is a test”
>puts x.sub(/..$/, ‘Hello’)

Output: This is a teHello

5.>”This is a test”.scan(/../) { |x| puts x }

Output:

Th
is
i
s
a
te
st

6. Basic Special Characters and Symbols Within Regular Expressions
Character Meaning
^ Anchor for the beginning of a line
$ Anchor for the end of a line
\A Anchor for the start of a string
\Z Anchor for the end of a string
. Any character
\w Any letter, digit, or underscore
\W Anything that \w doesn’t match
\d Any digit
\D Anything that \D doesn’t match (non-digits)
\s Whitespace (spaces, tabs, newlines, and so on)
\S Non-whitespace (any visible character)

7. “The car costs $1000 and the cat costs $10″.scan(/\d+/) do |x|
puts x
end

Output:

1000
10

8. >”The car costs $1000 and the cat costs $10”.scan(/^../) do |x| puts x end

Output: Th

9.> “The car costs $1000 and the cat costs $10″.scan(/../) do |x| puts x end

Output:

Th
e
ca
r
co
st
s
$1
00
0
an
d
th
e
ca
t
co
st
s
$1

10.>”The car costs $1000 and the cat costs $10″.scan(/..$/) do |x| puts x end

Output: 10

11.>”The car costs $1000 and the cat costs $10″.scan(/…$/) do |x| puts x end

Output: $10

12.>”The car costs $1000 and the cat costs $10″.scan(/\A../) do |x| puts x end

Output: Th

13.>”The car costs $1000 and the cat costs $10″.scan(/\B../) do |x| puts x end

Output:

he
ar
os
ts
$1
00
0
nd
he
at
os
ts
$1

14.>”The car costs $1000 and the cat costs $10″.scan(/\D../) do |x| puts x end

Output:

The
ca
r c
ost
s $
an
d t
he
cat
co
sts
$1

15.>”The car costs $1000 and the cat costs $10″.scan(/..\Z/) do |x| puts x end

Output: 10

16.>”The car costs $1000 and the cat costs $10″.scan(/\w/) do |x| puts x end

Output:

T
h
e
c
a
r
c
o
s
t
s
1
0
0
0
a
n
d
t
h
e
c
a
t
c
o
s
t
s
1
0

17.>”The car costs $1000 and the cat costs $10″.scan(/\W/) do |x| puts x end

Output:

$

$

18.>”The car costs $1000 and the cat costs $10″.scan(/\w\W/) do |x| puts x end

Output:

e
r
s
0
d
e
t
s

19.>”The car costs $1000 and the cat costs $10″.scan(/\d/) do |x| puts x end

Output:

1
0
0
0
1
0

Regular Expression Character and Sub-Expression Modifiers
Modifier Description

* Match zero or more occurrences of the preceding character, and match
as many as possible.

+ Match one or more occurrences of the preceding character, and match
as many as possible.

*? Match zero or more occurrences of the preceding character, and match
as few as possible.

+? Match one or more occurrences of the preceding character, and match
as few as possible.

? Match either one or none of the preceding character.

{x} Match x occurrences of the preceding character.

{x,y} Match at least x occurrences and at most y occurrences.

20. >”This is a test”.scan(/[aeiou]/) { |x| puts x }

Output:

i
i
a
e

21. >”This is a test”.scan(/[a-m]/) { |x| puts x }

Output:

h
i
i
a
e

22.> puts “String has vowels” if “This is a test” =~ /[aeiou]/

Output: String has vowels

23. >puts “String contains no digits” unless “This is a test” =~ /[0-9]/

Output: String contains no digits

24. >puts “String has vowels” if “This is a test”.match(/[aeiou]/)

Output: String has vowels

25. >puts “String has vowels” if “This is a test”.match(“[aeiou]”)

Output: String has vowels

26.> x = “This is a test”.match(/(\w+) (\w+)/)
puts x[0]
puts x[1]
puts x[2]

Output:

This is
This
is

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s