3 ms·
Just on the top of my head: 1. The flip-flop >> s = true >> (1..10).reject { true if (s = !s) .. (s) } => [1, 3, 5, 7, 9] 2. A snake in my Ruby?!
by judofyr 16y ago
Just on the top of my head:
1. The flip-flop
>> s = true
>> (1..10).reject { true if (s = !s) .. (s) }
=> [1, 3, 5, 7, 9]
2. A snake in my Ruby?!?
# Works in both Python and Ruby
print """Hello World"""
3. END vs at_exit
END {
puts "Ruby has END ..."
}
at_exit do
puts "... and at_exit"
end
puts "Do you know the difference?"
4. BEGIN/END wtf?
# Have you ever seen these in production code?
BEGIN {}
END {}
5. The wonders of a single space
# Returns the position where the regexp matches "foobar",
# or nil if no match.
def get(regexp = /bar/)
regexp =~ "foobar"
end
get /1#/ # => nil
get / 1#/ # => 3 <- WTF?
6. Local variable tables; don't we all love them?
def get(regexp = /bar/)
regexp =~ "foobar"
end
get /1#/ # => nil
get = 5
get /1#/ # => 5
get / 1#/ # => 5
- derefr 16y agoJust guessing that your #4, BEGIN and END, are for using Ruby like awk (that is, from the shell—you should never see them in a script.) Try this (and cringe): cat /usr/share/dict/words | ruby -lne 'puts $_.reverse; n = (n || 0) + 1; END{ puts "total: #{ n }" }'