Why I Love Coding in Ruby: Dir.mktmpdir
Burdette Lamar
Nov 3 '18
・1 min read
Because blocks!
This code creates a temporary directory and a file foo.txt therein:
require 'tmpdir'
Dir.mktmpdir do |dir|
open("#{dir}/foo.txt", 'w') do |file|
# Do something with the file.
end
end
When the block exits, the directory and its contents are deleted automatically.
Easy peasy!
56
16
Trending on
Don't Do It “Because Google Does It”
BASH tricks: The dir stack.
20 VS Code Extensions You'll Actually Use
Is there such thing as a bad programming language?
Weekend Hacking -- What Projects Are You Working on This Weekend?
It's Important To Test Your Code
Thing of beauty
What is your opinion on Ruby scripting verses other languages like Python or Bash? I know that Metasploit (vulnerability application) is programmed in ruby but I also have read that most people use Python or Bash for scripting. What are some advantages Ruby might have?
I don't think of Ruby as a scripting language, even though it's interpreted rather than compiled. In fact, I never say 'test script' b/c it seems to me to suggest something lightweight rather than substantial.
Bash is not object-oriented (disqualifying in my view), and therefore not at all comparable to Ruby, Python, or even Perl, all of which are object-oriented.
Besides blocks (which I love), Ruby just feels fluid to me.
Thanks for your honest response! :) I will keep that in mind for sure.