unzip (zip, tar, tag.gz) files with ruby

To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby: require ‘rubygems/package’ require ‘zlib’ tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(‘Path/To/myfile.tar.gz’)) tar_extract.rewind # The extract has to be rewinded after every iteration tar_extract.each do |entry| puts entry.full_name puts entry.directory? puts entry.file? # puts entry.read end tar_extract.close Each entry of type Gem::Package::TarReader::Entry … Read more

Ruby local variable is undefined

In Ruby local variables only accessible in the scope that they are defined. Whenever you enter/leave a Class, a Module or a Method definiton your scope changes in Ruby. For instance : v1 = 1 class MyClass # SCOPE GATE: entering class v2 = 2 local_variables # => [“v2”] def my_method # SCOPE GATE: entering … Read more

Is Sinatra multi threaded?

tl;dr Sinatra works well with Threads, but you will probably have to use a different web server. Sinatra itself does not impose any concurrency model, it does not even handle concurrency. This is done by the Rack handler (web server), like Thin, WEBrick or Passenger. Sinatra itself is thread-safe, meaning that if your Rack handler … Read more

How to install therubyracer gem on 10.10 Yosemite?

gem uninstall libv8 brew install v8 gem install therubyracer gem install libv8 -v ‘3.16.14.3’ — –with-system-v8 this is the only way it worked for me on 10.10 (ruby 2.1.2) Or try gem install libv8 -v ‘XX.XX.XX’ — –with-system-v8 adding the version of the gem 🙂 UPDATE for Mac OS Catalina: brew tap homebrew/versions brew install … Read more

Difference between $stdout and STDOUT in Ruby

$stdout is a global variable that represents the current standard output. STDOUT is a constant representing standard output and is typically the default value of $stdout. With STDOUT being a constant, you shouldn’t re-define it, however, you can re-define $stdout without errors/warnings (re-defining STDOUT will raise a warning). for example, you can do: $stdout = … Read more

How do I compare two hashes?

You can compare hashes directly for equality: hash1 = {‘a’ => 1, ‘b’ => 2} hash2 = {‘a’ => 1, ‘b’ => 2} hash3 = {‘a’ => 1, ‘b’ => 2, ‘c’ => 3} hash1 == hash2 # => true hash1 == hash3 # => false hash1.to_a == hash2.to_a # => true hash1.to_a == hash3.to_a … Read more

Ruby : How to write a gem? [closed]

Rubygems.org’s Guides is one of the best resources for writing your own gem. If you’re using Bundler in your app, you might want to look at Ryan Bigg’s guide to Developing a RubyGem using Bundler and the Railscast on creating gems with Bundler. If you’re interested in tools to help you write gems: Jeweler – … Read more

Should I use alias or alias_method?

alias_method can be redefined if need be. (it’s defined in the Module class.) alias‘s behavior changes depending on its scope and can be quite unpredictable at times. Verdict: Use alias_method – it gives you a ton more flexibility. Usage: def foo “foo” end alias_method :baz, :foo