Ruby string to octal?
How can I convert "755" to 0755 in Ruby? I want to pass perm开发者_开发百科issions to a method using a string and then convert it for chmod use.
This should do it:
"755".to_i(8)
# => 493
"755".to_i(8) == 0755
# => true
A bit late to the party, but you can check for input errors by passing the string and base to instantiate an Integer thus,
Integer("755",8)=493
Integer("855",8)
ArgumentError: invalid value for Integer(): "855"
begin
Integer("855",8)
rescue ArgumentError, TypeError
"Bad input"
end
def append_zero_to_string(string)
0.to_s + string
end
精彩评论