开发者

Ruby string to octal?

开发者 https://www.devze.com 2023-01-12 03:49 出处:网络
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:

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

精彩评论

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