Ruby On Rails Simple Examples & Strings
Ruby On Rails Simple Examples & Strings
Ruby On Rails Simple Examples & Strings
Today we will look into few examples and introduce on strings with working examples.
n! = 1 (when n==0)
= n * (n-1)! (otherwise)
def factorial(n)
if n == 0
1
else
n * factorial(n-1)
end
end
(Note: the syntax of ruby more closely mimics that of a language named Eiffel.)
You may also notice the lack of a return statement. It is not needed because a ruby
function returns the last thing that was evaluated in it. Though use of a return statement
is permissible but it is not necessary here.
Let's try out our factorial function. Adding one line of code gives us a working program:
def factorial(n)
if n == 0
1
else
n * factorial(n-1)
end
end
ARGV is an array which contains the command line arguments, and to_i converts a
character string to an integer.
Experiment:
What happens if I pass string as argument instead of integer?
At the line “12”, replace the double quote with single quote and see what happens?(It
will be discussed in next page)
Because of aliasing issues, users of strings should be aware of the methods that modify
the contents of a String object.
Ruby deals with strings as well as numerical data. A string may be double-quoted ("...")
or single-quoted ('...').
Examples:
If you had done the above mentioned experiments, it is easy to understand the above two
points. The difference between the double and single quotes around a string. There are
plenty of methods that ruby offers, they come very handy!
capitalize:
This method turns the first letter of the string to upper case.
Ruby
chomp:
In Ruby, strings are mutable. They can expand as needed, without using much time and
memory. Ruby stores a string as a sequence of bytes.
insert:
str.insert(index, other_str) => str
Inserts other_str before the character at the given index, modifying str. Negative indices
count from the end of the string, and insert after the given character. The intent is insert
aString so that it starts at the given index.
"abcd".insert(0, 'X') #=> "Xabcd"
"abcd".insert(3, 'X') #=> "abcXd"
length:
str.length => integer
Returns the length of str.
"ruby". length #=> 4
"ruby on rails". length #=> 13
ljust:
str.ljust(integer, padstr=' ') => new_str
If integer is greater than the length of str, returns a new String of length integer with str
left justified and padded with padstr; otherwise, returns str.
"hello".ljust(4) #=> "hello"
"ruby".ljust(20, '1234') #=> "ruby1234123412341234"
strip:
str.lstrip => new_str
Returns a copy of str with leading whitespace removed.
" hello ".lstrip #=> "hello "
"hello".lstrip #=> "hello"
replace:
str.replace(other_str) => str
Replaces the contents of str with the corresponding values in other_str.
s = "java" #=> "java"
s.replace "Ruby" #=> "Ruby"
reverse:
capitalize!:
str.capitalize! => str or nil
Modifies str by converting the first character to uppercase and the remainder to
lowercase. Returns nil if no changes are made.
a = "hello"
a.capitalize! #=> "Hello"
a #=> "Hello"
a.capitalize! #=> nil
Following is the list of methods that ruby supports for the String Class.
Experiment:
to_f, to_i, to_s, to_str, to_sym, to_yaml toeuc tojis tosjis toutf16 toutf8 tr
tr, tr!, tr!, tr_s, tr_s!,
References:
Ruby String Class: http://ruby-doc.org/core/classes/String.html