Ruby中的任何函数都决定了文件夹级别之间的区别?
发布时间:2020-12-17 02:59:20 所属栏目:百科 来源:网络整理
导读:我想要做的是弄清楚一些文件夹是否是儿童/父母之一或不相关. Ruby File API中是否有任何功能?如果不是如何搞清楚? 例如: real_path_1 = "/Users/amr/Code/xx/zz"real_path_2 = "/Users/amr/Code/"# when I have to compare between these paths like:f_le
我想要做的是弄清楚一些文件夹是否是儿童/父母之一或不相关.
Ruby File API中是否有任何功能?如果不是如何搞清楚? 例如: real_path_1 = "/Users/amr/Code/xx/zz" real_path_2 = "/Users/amr/Code/" # when I have to compare between these paths like: f_level(real_path_1,real_path_2) # it should return +2 # that means real_path_1 is **parent** of real_path_2 # with 2 levels depth 另一个例子: real_path_1 = "/Users/amr/Code/" real_path_2 = "/Users/amr/Code/foo/bar/inside" # when I have to compare between these paths like: f_level(real_path_1,real_path_2) # it should return -3 # that means real_path_1 is **child** of real_path_2 # with 3 levels depth 另一个例子: real_path_1 = "/Users/amr/Code/" real_path_2 = "/Users/amr/Code/" # when I have to compare between these paths like: f_level(real_path_1,real_path_2) # it should return 0 # that means real_path_1 is **same level** as real_path_2 当返回nil时: real_path_1 = "/Users/other/Code/" # or "/folder2/" real_path_2 = "/Users/amr/Code/" # or "/folder1/" # when I have to compare between these paths like: f_level(real_path_1,real_path_2) # it should return nil # that means real_path_1 is **not at same level** as real_path_2 解决方法def f_level a,b if a[/^#{b}/] && (b[-1,1] == '/' || $'[0,1] == '/') $'.scan(/[^/]+/).size elsif b[/^#{a}/] && (a[-1,1] == '/') -($'.scan(/[^/]+/).size) end end "/Users/amr/Code/xx/zz" "/Users/amr/Code/" 2 "/Users/amr/Code/" "/Users/amr/Code/xx/zz" -2 "/Users/amr/Code/" "/Users/amr/Code/" 0 "/Users/other/Code/" "/Users/amr/Code/" nil (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |