irb中<<左位移操作符问题
如果在IRB中调以下代码会出现类here document的作用,而不是直接进行位移操作,虽然结果最后还是对的。
irb(main):020:0> a = 2
irb(main):020:0> c = a <<1
irb(main):021:0" 2
irb(main):022:0" 1
=> 1
irb(main):023:0> c
=> 4
如果a <<1中1之前再加个半角空格,那会正常运算,不知是否属于IRB的Bug,在Ruby脚本中直接运行是正常的。
如果在IRB中调以下代码会出现类here document的作用,而不是直接进行位移操作,虽然结果最后还是对的。
irb(main):020:0> a = 2
irb(main):020:0> c = a <<1
irb(main):021:0" 2
irb(main):022:0" 1
=> 1
irb(main):023:0> c
=> 4
如果a <<1中1之前再加个半角空格,那会正常运算,不知是否属于IRB的Bug,在Ruby脚本中直接运行是正常的。
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...
发表者
俞 伟军
位置在:
7/03/2007 10:54:00 PM
0
评论