I m在案卷上工作,卸载部分铁路机。 不同类型的档案由附录处理。
我想把某些档案延期列为一个白色清单,以检查已上载的档案,看看它们应该去什么地方。 所有档案名称都是插图。
我需要一种办法,只检查档案名称拼写的延伸部分。 档案名称均采用“一些_file_name.some_extension”的形式。
I m在案卷上工作,卸载部分铁路机。 不同类型的档案由附录处理。
我想把某些档案延期列为一个白色清单,以检查已上载的档案,看看它们应该去什么地方。 所有档案名称都是插图。
我需要一种办法,只检查档案名称拼写的延伸部分。 档案名称均采用“一些_file_name.some_extension”的形式。
irb(main):002:0> accepted_formats = [".txt", ".pdf"]
=> [".txt", ".pdf"]
irb(main):003:0> File.extname("example.pdf") # get the extension
=> ".pdf"
irb(main):004:0> accepted_formats.include? File.extname("example.pdf")
=> true
irb(main):005:0> accepted_formats.include? File.extname("example.txt")
=> true
irb(main):006:0> accepted_formats.include? File.extname("example.png")
=> false
使用<条码>分号<>。 方法 档案组
File.extname("test.rb") #=> ".rb"
也可请<条码> 方法
File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
某些老话题,但这里是消除延伸隔离器狗和可能的拖车空间的途径:
File.extname(path).strip.downcase[1..-1]
实例:
File.extname(".test").strip.downcase[1..-1] # => nil
File.extname(".test.").strip.downcase[1..-1] # => nil
File.extname(".test.pdf").strip.downcase[1..-1] # => "pdf"
File.extname(".test.pdf ").strip.downcase[1..-1] # => "pdf"
我认为,这样做比较容易去除延伸分母。
File.extname(path).delete( . )
Yet another approach, using delete_suffix
and delete_prefix
. This is easier to read, IMO.
filename = path/to/file.ext
file_name_only = File.basename(filename).delete_suffix(File.extname(filename))
file_ext = File.extname(filename).delete_prefix .
file_path = File.dirname(filename)
new_name = "#{file_path}/#{file_name_only}_blahblah.#{file_ext}"
In IRB:
$ irb
irb(main):001:0> filename = path/to/file.ext
=> "path/to/file.ext"
irb(main):002:0> file_name_only = File.basename(filename).delete_suffix(File.extname(filename))
=> "file"
irb(main):003:0> file_ext = File.extname(filename).delete_prefix .
=> "ext"
irb(main):004:0> file_path = File.dirname(filename)
=> "path/to"
irb(main):005:0> new_name = "#{file_path}/#{file_name_only}_blahblah.#{file_ext}"
=> "path/to/file_blahblah.ext"
irb(main):006:0>
这个员额回答了我的问题,但我的使用情况恰恰相反。 我想找到档案的名称,而没有延期。 我发现文档名称有<代码>File.basename,然后将<编码>File.extname与gsub
合并,以删除。 同样:
@file = /path/to/my-file-name.md
File.basename(@file).gsub(File.extname(@file), )
# => my-file-name
collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...
We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...
As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...
I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...
I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml
guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...
I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...