How to get filename without extension from file path in Ruby

Try File.basename Returns the last component of the filename given in file_name, which must be formed using forward slashes (“/’’) regardless of the separator used on the local file system. If suffix is given and present at the end of file_name, it is removed. File.basename(“/home/gumby/work/ruby.rb”) #=> “ruby.rb” File.basename(“/home/gumby/work/ruby.rb”, “.rb”) #=> “ruby” In your case: File.basename(“C:\\projects\\blah.dll”, … Read more

Read binary file as string in Ruby

First, you should open the file as a binary file. Then you can read the entire file in, in one command. file = File.open(“path-to-file.tar.gz”, “rb”) contents = file.read That will get you the entire file in a string. After that, you probably want to file.close. If you don’t do that, file won’t be closed until … Read more

Ruby: Easiest Way to Filter Hash Keys?

Edit to original answer: Even though this is answer (as of the time of this comment) is the selected answer, the original version of this answer is outdated. I’m adding an update here to help others avoid getting sidetracked by this answer like I did. As the other answer mentions, Ruby >= 2.5 added the … Read more

What is the difference between print and puts?

puts adds a new line to the end of each argument if there is not one already. print does not add a new line. For example: puts [[1,2,3], [4,5,nil]] Would return: 1 2 3 4 5 Whereas print [[1,2,3], [4,5,nil]] would return: [[1,2,3], [4,5,nil]] Notice how puts does not output the nil value whereas print … 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