4 ms·
Not the purpose of the article but as I am a bit negative on Ruby, and I am neutral to positive on Java... "Ruby is so much better than Java! Look at how you o
by belter 4y ago
Not the purpose of the article but as I am a bit negative on Ruby, and I am neutral to positive on Java...
"Ruby is so much better than Java! Look at how you open a file in Java (shows you 30 lines) vs. Ruby! (2 lines)"
Ruby Example.
================
begin
File.open("/path/to/your/file.txt", "r") do |file|
file.each_line do |line|
puts line
end
end
rescue Errno::ENOENT
puts "File not found"
rescue Exception => e
puts "An error occurred: #{e.message}"
end
Java Example, one line less than Ruby ( no imports shown but could also be required for external Gems )
================
public class Main {
public static void main(String[] args) {
String filePath = "/path/to/your/file.txt";
try (Stream<String> lines = Files.lines(Paths.get(filePath))) {
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- wmoxam 4y agoThe two aren't equivalent (you're using a catchall exception handler in Ruby but not in Java, and are printing a stack trace in java but not in Ruby). Here's the equivalent Ruby: begin File.readlines('/path/to/your/file.txt').each {|line| puts line} rescue Errno::ENOENT => e puts e.backtrace end
- xigoi 4y agoYour Ruby example is unnecessarily complicated. Why not just use File.read? Why are you distinguishing two different kinds of exceptions, but not in the case of Java? And what if you don't need error handling?
- nightpool 4y agoIf you're writing a quick script, there's absolutely no point to caching those exceptions in Ruby—it's just going to cause your program to continue when it otherwise should have quit out due to an error condition outside of its control. Java's checked exceptions breeds overly defensive programming like this. Anyway, here's a slightly better ruby version: File.readlines('file.txt').each do |line| puts(line) end Notice how I don't have to worry at all about constructing or manipulating a Path object, which I find to always be a very tedious part of file IO in Java. It's fully correct, sure, but for a web server that's only ever going to be deployed on Linux containers or a 50 line CLI script that's trying to wring a useful task out of some Java-only library, it's just insane overkill. The only thing worse then using Paths objects is trying to figure out which file calls do and don't work with path objects and which need to be converted to some other class that does the exact same thing. Notice also how this is much more equivalent to the Java example provided, except it also consumes the file so you don't have to worry about introducing a new syntax construct to understand how the file gets closed. (Same, incidentally, with the File.open case, which just reuses a standard ruby block instead of introducing a new magic try statement that autocloses the file). You lose laziness, but you can get it back by using File.foreach instead, at the cost of a little bit of readability
- srpablo 4y agohaha yeah, my comment was mostly a throwback to the genre of articles we saw in the late aughts where people were discovering dynamic programming environments, and would say the line about Ruby/Java but not mention all the cases the Java case was handling that their Ruby `File.read` example didn't. I put it in quotes not to make that exact point, but more to reference how I've seen the conversation go. Generally I'm extremely positive on Java :-p