- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试学习 ruby(1.8.7)。我用 php 编程有一段时间了,我认为自己精通这种语言。我有一本我引用的书,我看过很多介绍性的 ruby 教程,但我无法理解这一点。
myFiles = ['/Users/', '/bin/bash', 'Phantom.file']
myFiles.each do |cFile|
puts "File Name: #{cFile}"
if File.exists?(cFile)
puts "#{cFile} is a file"
cFileStats = File.stat(cFile)
puts cFileStats.inspect
cFileMode = cFileStats.mode.to_s()
puts "cFileMode Class: " + cFileMode.class.to_s()
puts "length of string: " + cFileMode.length.to_s()
printf("Mode: %o\n", cFileMode)
puts "User: " + cFileMode[3,1]
puts "Group: " + cFileMode[4,1]
puts "World: " + cFileMode[5,1]
else
puts "Could not find file: #{cFile}"
end
puts
puts
end
产生以下输出:
File Name: /Users/
/Users/ is a file
#<File::Stat dev=0xe000004, ino=48876, mode=040755, nlink=6, uid=0, gid=80, rdev=0x0, size=204, blksize=4096, blocks=0, atime=Sun Sep 04 12:20:09 -0400 2011, mtime=Thu Sep 01 21:29:08 -0400 2011, ctime=Thu Sep 01 21:29:08 -0400 2011>
cFileMode Class: String
length of string: 5
Mode: 40755
User: 7
Group: 7
World:
File Name: /bin/bash
/bin/bash is a file
#<File::Stat dev=0xe000004, ino=8672, mode=0100555, nlink=1, uid=0, gid=0, rdev=0x0, size=1371648, blksize=4096, blocks=1272, atime=Sun Sep 04 16:24:09 -0400 2011, mtime=Mon Jul 11 14:05:45 -0400 2011, ctime=Mon Jul 11 14:05:45 -0400 2011>
cFileMode Class: String
length of string: 5
Mode: 100555
User: 3
Group: 3
World:
File Name: Phantom.file
Could not find file: Phantom.file
为什么字符串长度与预期不同? (对于用户应该是 5,对于/bin/bash 应该是 6)?为什么子串没有拉出正确的字符。我知道在引用 5 个字符的字符串时没有填充 World,但是偏移量似乎不存在,并且在/bin/bash 的情况下,3 甚至没有出现在字符串中。
谢谢
斯科特
最佳答案
这个不错。
当一个数字前面有一个 0 时,它被表示为八进制。你实际得到的 bin/bash 是什么:
0100755 to decimal = 33261
"33261".length = 5
对于/用户:
040755 to decimal = 16877
"16877".length = 5
添加以下行:
puts cFileMode
你会看到错误。
to_s takes an argument which is the base .如果你调用 to_s(8) 那么它应该可以工作。
cFileMode = cFileStats.mode.to_s(8)
编辑
files = ['/home/', '/bin/bash', 'filetest.rb']
files.each do |file|
puts "File Name: #{file}"
if File.exists?(file)
puts "#{file} is a file"
file_stats = File.stat(file)
puts file_stats.inspect
file_mode = file_stats.mode.to_s(8)
puts "cFileMode Class: #{file_mode.class}"
p file_mode
puts "length of string: #{file_mode.length}"
printf("Mode: #{file_mode}")
puts "User: #{file_mode[-3,1]}"
puts "Group: #{file_mode[-2,1]}"
puts "World: #{file_mode[-1,1]}"
else
puts "Could not find file: #{file}"
end
puts
puts
end
关于ruby - Ruby 中的字符串转换和子字符串让我感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7301960/
我正在尝试学习Rust。我正在阅读一本书online,该书实现了unix程序cat。现在,我试图读取作为像cargo run file1.txt file2.txt这样的参数传递的文件的内容,但是程序
我在 GHC 8.0.1 中遇到了一个带有种类索引 (?) GADT 的奇怪情况,其中在类型与种类签名中引入 foralls 会产生不同的类型检查行为。 考虑以下数据类型: {-# LANGUAGE
我正在使用 Perl 5.10 开发应用程序,HTML::Mason和 Apache 2.2。这是我第一次在大型项目中使用 Perl 5.10。我每隔一段时间就会出现奇怪的行为。应用程序因一个非常奇怪
我正在尝试将文件上传到aws中的rust中,因为我使用的是 rusoto_s3 的s3 rust客户端,当这些部分从单个线程发送时,我设法使分段上传代码正常工作不是我想要的,我想上传大文件,并且希望能
我是一名优秀的程序员,十分优秀!