Ruby基本用法

359 阅读1分钟

1 文件操作

1.1 当前用户跟目录

UI.puts "Dir.home: #{Dir.home}"

1.2 目录是否存在

File.directory?(path)

如果 path 是一个目录,则返回 true

1.2 路径是否存在

File.exist?(path)

如果 path 存在,则返回 true

1.3 当前目录的父目录

# 方式 1
File.expand_path("..", Dir.pwd)
# 方式 2
File.dirname(Dir.pwd)

2、打印日志

aa = system('ls')    # system 只返回是否执行成功,为 true 表示执行成功,其他值表示执行失败
puts aa
puts '-----------------------'
bb = %x(ls)         # 使用 %x 可以获取命令执行的输出结果
puts bb

结果:

test.rb  test.txt  world.rb
true
-----------------------
test.rb
test.txt