Tips on the Ruby Programming Language
Posted on Aug 06, 2012 in Computer Science
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
Array
-
The method
push
inserts an element to the back of the arry; the methodinsert
inserts an element to any place of the array. -
The method
pop
removes the last element of the array; the methoddelete
removes any specified element from the array. -
The method
concat
combines the array with another one; the methodjoin
concatenates all elements in the array separated by the specified delimiter.
String
-
Use the method
to_i
to convert a string to integer. -
You use both single and double quotes to denote string. The difference is that single quotes preserves escape characters while double quotes interprete escape characters.
-
The method
end_with?
check whether a string ends with the specified string. -
The method
upcase
returns a copy of the string with lower case letters replaced with their upper case letters; the methoddowncase
returns a copy of the string with upper case letters replaced with their lower case letters; the methodswapcase
return a copy of the string with lower case letters replaced with their upper case letters and upper case character replaced with their lower case letters. -
Use the method
strip
to remove leading and trailing white spaces.
Input and Output
-
Both the function
puts
andprint
display the content of objects on the console. The difference is that the functionputs
add a new line to the end if their is no one whileprint
not. -
The function
gets
read in input from the console. You can use thechomp
method to ignore new lines, i.e., you can usegets.chomp
to read a single line from the console.
File and Directory
-
You can use the method
Dir.entries
to query files and subdirectories in a directory. -
The method
File.delete
removes files specified files. The number of files passed as arguments is returned.
Flow Control
- You cannot use
else if
in Ruby, instead you can useelsif
.
Object and Class
-
Use
methods
to query all methods of an object. -
You'd not define a function with the same as some directly usable function/method in Ruby.