开发者

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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜