Read without ads and support Scribd by becoming a Scribd Premium Reader.
 
Ruby on Rails : StringsToday we will look into few examples and introduce on strings with working examples.Say we start on simple math factorial functionThe mathematical definition of 
n
factorial is:
n! = 1 (when n==0)= n * (n-1)! (otherwise)
In ruby, this can be written as:
def factorial(n)if n == 01elsen * factorial(n-1)endend
You may notice the repeated occurrence of 
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 rubyfunction returns the last thing that was evaluated in it. Though use of a
return
statementis permissible but it is not necessary here.Let's try out our factorial function. Adding one line of code gives us a working program:
# Program to calculate the factorial of a number# Save this as factorial.rbdef factorial(n)if n == 01elsen * factorial(n-1)endendprint factorial(ARGV[0].to_i), "\n"ARGV
is an array which contains the command line arguments, and
to_i
converts acharacter string to an integer.1Prepared By: Sumanth Krishna. A
 
Ruby on Rails : Strings
 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)
2Prepared By:Sumanth Krishna. A
 
Ruby on Rails : StringsA
String
object holds and manipulates an arbitrary sequence of bytes, typicallyrepresenting characters. String objects may be created using
String::new
or as literals.Because of aliasing issues, users of strings should be aware of the methods that modifythe contents of a
String
object.
Methods with names ending in ``!’’ modify their receiver.
Methods without a ``!’’ return a new
String
.Ruby deals with strings as well as numerical data. A string may be double-quoted ("...")or single-quoted ('...').
A double-quoted string allows character escapes by a leading backslash, and theevaluation of embedded expressions using
#{}
.
A single-quoted string does not do this interpreting; what you see is what you get.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:
str.capitalize => new_str This method turns the first letter of the string to upper case.
"hello".capitalize #=> "Hello"
Ruby
chomp:
str.chomp(separator=$/) => new_str Returns a new
String
with the given record separator removed from the end of 
 str 
(if  present). If 
$/
has not been changed from the default Ruby record separator, then
chomp
also removes carriage return characters (that is it will remove
\n
,
\r
, and
\r\n
).
"ruby\r\n".chop #=> "string""ruby\n\r".chop #=> "string\n"
3Prepared By:Sumanth Krishna. A
Search History:
Searching...
Result 00 of 00
00 results for result for
  • p.
  • Notes
    Load more