1) If you're reading bad code, that could be a problem. But 99% of Ruby code I read has been written by people who know when they need parentheses/brackets to make the code readable (e.g. when it's not the only code on the line.) But there are tons of times when it's wonderful to be able to leave out the parentheses.
-
2) It might be because I spend a lot of time with Ruby, but I much prefer `end`s to curly brackets, they're just easier on the eyes, especially when they're going to take up the entire line anyway:
a
b
c
d
end
end
end
end
a {
b {
c {
d {
}
}
}
}
Of course, at times I wish Ruby used indentation to declare structure, like Python.
-
3) I don't think I've ever had that problem. If I'm calling `person.full_name`, I don't care if that's a variable, or a method joining first_name and last_name. And if I it was a method, ideally I'd have a corresponding `full_name=(str)` method defined too, so `person.full_name = "Joe Bloggs"` would work, and split the string into the first, last, and optional middle names.
-
4) Why? Why not? What makes this easier to read?
puts "Bankrupt" if balance <= 0.0
Ruby is a lot like the English language; when you're learning it as a second language, you're constantly confused and frustrated by it's weird syntax, but once you get past those initial hurdles, it just clicks. I love Ruby because I never feel like I'm programming in it, it almost feels like pseudo code that's not going to actually execute.
Essentially you could get any non-programmer to solve a problem on paper, and turn it into Ruby code which, without too much fuss, describes their process.
# get the customers text file
# each customer is on their own line, w/ their name and age separated by a comma
# group the customers by age
# show me the top 3 most common ages
file = File.open('customers.txt')
customers = []
file.each_line do |line|
name, age = line.split(",")
customers << {:name => name, :age => age.to_i}
end
grouped_ages = customers \
.group_by{ |c| c[:age] } \
.map{ |c| [c[0], c[1].size] } \
.sort_by(&:last) \
.reverse
grouped_ages.first(3).each do |age, size|
puts "#{age} years old: #{size} customers"
end
Four blocks of reasonably clean code (except the 3rd, that could be cleaned up a bit), which implement the four lines of plain english instructions.