Lesson content is currently in draft form.
Label our work
Things are getting complicated and ugly. The code we wrote in the last chapter to find the user with the highest rate of tweets was simple in conception, but pretty ugly visually. You may remember all the steps if you just wrote them five minutes ago, but you’re going to struggle when you review the code tomorrow.
We’ve been using variables to “label our data”. We’re going to do the same thing with our routines: wrapping them up in a method call that can be invoked with a human-readable name.
How methods are made
Here’s a very simple method:
1 2 3 |
|
The return value
The return
keyword designates the value returned by the method In Ruby, if return
isn’t used, the method automatically returns what was in the last line.
(If you invoke return
before the last line of a method, the method will exit at that point.)
In the hello_world
method above, the return value is "Hello world"
, a string
Methods with arguments
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Naming methods
Like variables, you want your method names to act as a quick definition of what they contain. So a method named do_work
is virtually worth it. But saves_report_to_disk
is pretty good, as it tells you what you need to know about the method’s effect.
So it’s OK to keep method names on the long side. How long is too long? It’s really not about length, but about design. For example:
calculates_new_totals_and_saves_report_to_disk
The problem with that method is not the length of its name, per se, but that the method is doing too much: its long descriptive name is only a symptom of the problem.
A better design pattern is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
ruby def hello_world
a_word = "Bonjour"
b_word = "Earth"
return a_word + " " + b_word
end
puts a_word
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
ruby
def calc_twitter_user_age_seconds(user_hash)
# pre: user_hash is a Hash containing Twitter account info, including a 'created_at' attribute
# returns: number of seconds since account created, as a decimal
a = Time.now - Time.parse(user_hash['created_at'])
return a.to_f
end
1
|
|
ruby
def calc_twitter_user_tweet_rate(user_hash)
# pre: user_hash is a Hash containing Twitter account info
# returns: returns the number of tweets per second
return user_hash['statuses_count'].to_f / calc_twitter_user_age_seconds(user_hash)
end
1 2 |
|
ruby def calc_twitter_user_age_days(user_hash)
# pre: user_hash is a Hash containing Twitter account info
# returns: returns the number of days since the account was created
return calc_twitter_user_age_seconds(user_hash) / (60 * 60 * 24)
end
1
|
|
ruby def calc_twitter_user_tweet_rate(user_hash)
# pre: user_hash is a Hash containing Twitter account info
# returns: returns the number of tweets per days
return user_hash['statuses_count'].to_f / calc_twitter_user_age_days(user_hash)
end
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Note: IF you want to, you can really abstract out the code and write a find_most_tweety_member
method which takes in an array of Twitter account data and returns the one with the highest tweet rate. There’s a better way to do that though, as we’ll see in later chapters.