Extending irb [programming ruby chapter15]
Because the things you type to irb are interpreted as Ruby code, you can effectively 
extend irb by defining new top-level methods. For example, you may want to be able to 
look up the documentation for a class or method while in irb. If you add the following to 
your .irbrc file, you’ll add a method called ri, which invokes the external ri command 
on its arguments. 
def ri(*names) 
  system(%{ri #{names.map {|name| name.to_s}.join(" ")}}) 
end 
The next time you start irb, you’ll be able to use this method to get documentation. 
irb(main):001:0> ri Proc 
--------------------------------------------------------- Class: Proc 
Proc objects are blocks of code that have been bound to a set of 
local variables. Once bound, the code may be called in different 
contexts and still access those variables. 
and so on... 
irb(main):002:0> ri :strftime 
------------------------------------------------------- Time#strftime 
time.strftime( string ) => string 
--------------------------------------------------------------------- 
Formats time according to the directives in the given format 
string. Any text not listed as a directive will be passed through 
to the output string. 
Format meaning: 
%a - The abbreviated weekday name (``Sun'') 
%A - The full weekday name (``Sunday'') 
%b - The abbreviated month name (``Jan'') 
%B - The full month name (``January'') 
%c - The preferred local date and time representation 
%d - Day of the month (01..31) 
and so on... 
irb(main):003:0> ri "String.each" 
--------------------------------------------------------- String#each 
str.each(separator=$/) |substr| block => str 
str.each_line(separator=$/) |substr| block => str 
--------------------------------------------------------------------- 
Splits str using the supplied parameter as the record separator 
($/ by default), passing each substring in turn to the supplied 
block. If a zero-length record separator is supplied, the string 
is split on \n characters, except that multiple successive 
newlines are appended together. 
print "Example one\n" 
"hello\nworld".each |s| p s 
and so on...
No comments :
Post a Comment