use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let query = if args.len() > 1 {
args[1..].join(" ")
} else {
String::new()
};
println!("ri - Ruby documentation viewer");
if query.is_empty() {
println!("Usage: ri <class|method|module>");
println!("Example: ri String#length");
println!(" ri Array");
return;
}
lookup_documentation(&query);
}
fn lookup_documentation(query: &str) {
println!("Looking up documentation for: {}", query);
match query {
"String" => {
println!("\n= String");
println!("\nRuby's String class represents character strings.");
println!("\nMethods:");
println!(" length() - Returns the length of the string");
println!(" empty?() - Returns true if the string is empty");
println!(" upcase() - Returns a new string with all uppercase letters");
println!(" downcase() - Returns a new string with all lowercase letters");
}
"String#length" => {
println!("\n= String#length");
println!("\nReturns the length of the string.");
println!("\nExamples:");
println!(" 'hello'.length # => 5");
println!(" ''.length # => 0");
}
"Array" => {
println!("\n= Array");
println!("\nRuby's Array class represents ordered, integer-indexed collections of any object.");
println!("\nMethods:");
println!(" length() - Returns the number of elements in the array");
println!(" push() - Appends an object to the end of the array");
println!(" pop() - Removes and returns the last element of the array");
println!(" each() - Calls the given block once for each element");
}
"Array#each" => {
println!("\n= Array#each");
println!("\nCalls the given block once for each element in self.");
println!("\nExamples:");
println!(" [1, 2, 3].each {{ |i| puts i }} # prints 1, 2, 3");
}
_ => {
println!("Documentation not found for: {}", query);
println!("Try another query or run 'ri' for usage.");
}
}
}