loading...

Why I Love Coding in Ruby: Dir.mktmpdir

github logo ・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!

twitter logo DISCUSS (4)
markdown guide
 
 

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.

Classic DEV Post from Sep 30 '19

Hacktoberfest for beginners - Low hanging contribution opportunities

Burdette Lamar profile image
Started out teaching English at Embry-Riddle. Graded 10,000 essays. Lesson learned. Became a mathematics teacher. Discovered computing. Never looked back.