3 ms·
His Ruby for line in ARGF for word in line.split if word.match(/ing$/) then puts word end end end I'd write as
by jules 16y ago
His Ruby
for line in ARGF
for word in line.split
if word.match(/ing$/) then
puts word
end
end
end
I'd write as
for line in ARGF
puts line.split.grep(/ing$/)
end
Or
puts ARGF.map{|line| line.split.grep(/ing$/)}
- epochwolf 16y agoOr ARGF.each {|line| line.scan(/\b\w+ing\b/).each(&:puts) } You know, why are we breaking this by line? :P ARGF.read.scan(/\b\w+ing\b/).each(&:puts) Probably requires ActiveSupport for some versions of ruby :) The &:symbol trick isn't in all versions.
- petercooper 16y agoThe Python implementation doesn't use ARGF and so appears clearer. Luckily this is no problem in Ruby too: while line = gets puts line.split.select { |word| word =~ /ing$/ } end
- swolchok 16y agoThis is clearly not as bad as Perl, but it's still raving a bit. My key complaints are the complete lack of clarity as to what kind of thing split is, as well as what exactly is going on with select. (I'm assuming it's getting passed a predicate in the form of a lambda or block or whatever the cool kids are calling it these days, and it's obvious to the Perl programmer what the block does after that.) The equivalent Python is something like import sys for line in sys.stdin: print '\n'.join(filter(line.split(), lambda word: word.endswith('ing')) which I would argue is more readable to a layperson because function calls look like function calls. (By the way, does Ruby's puts automatically do the join with newlines, or what?)
- petercooper 16y agoMy key complaints are the complete lack of clarity as to what kind of thing split is The post seems to be about language independent readability. Rather than "functions should be called with () - that is clearly not a function - can not compute!!", the question is "can this code be understood even ignoring unfamiliar syntax?" Most developers familiar with languages like Java, JavaScript, Python, Perl, C# or Ruby could correctly guess what "split" refers to and that it is unlikely to be used as a variable name. Further, if a developer is familiar with the idea of method calls, it will be inferred that "whatever.split.whatever" is a sequence of method calls.. much as if it were, in some fictional languages, whatever->split->whatever or whatever:split:whatever. Similarly, a(b(c())) and a[b[c[]]] could both be easily inferred to be a set of nested function calls. "import sys" is not exactly clear either, but again, the meaning of this could be accurately inferred by most developers. print '\n'.join(filter(line.split(), lambda word: word.endswith('ing')) I want to join the array's elements together, not join the string. Joining together an array's elements by calling a method on the delimiter is no less raving than any Ruby I may cook up. Your example is both longer and has more syntax.
- jules 16y agoDoes it matter what kind of thing split is? There is no difference between properties and methods in Ruby. Yes, puts joins newlines.