3
\$\begingroup\$

I'm fairly new to Ruby, and I've been refactoring this code (it's a silly app, but it's helping me learn) as I learn more and more best practices and features new to Ruby 1.9 (e.g., hashrocket alternative). I figure having more experienced Rubyists give feedback on the short program will help me learn faster. So, with that in mind, is there anything in this code that isn't a best practice, or anything not so clean about it?

#!/usr/bin/env ruby

require 'twitter'

# Enter your creds
creds = {username:       'YourUsername',
         consum_key:     'SomeLongKeyBlahBlah', 
         consum_secret:  'AnotherLongKeyButASecretOneThisTime',
         oauth_token:    'OAuthTokenKeyBlahBlah',
         oauth_secret:   'OAuthSecretTokenOfSecrets'}

# Twitter auth
def twitterAuth(creds)
  Twitter.configure do |config|
      config.consumer_key       = creds[:consum_key] 
      config.consumer_secret    = creds[:consum_secret]
      config.oauth_token        = creds[:oauth_token]
      config.oauth_token_secret = creds[:oauth_secret]
  end
end

def sendTweet(creds)
  # Make string
  msg = '@' + creds[:username] + ' ' + ARGV.join(' ') 
  # Send!
  Twitter.update(msg) 
  # Return tweet
  puts 'Your tweet: ' + msg 
end

if __FILE__ == $0
  twitterAuth(creds)
  sendTweet(creds)
end
\$\endgroup\$
5
\$\begingroup\$

Generally Rubyists define methods in the underscore-lowercase pattern: def twitter_auth, def send_tweet. Additionally most that I've encountered prefer to drop the () from method calls: twitter_authenticate creds; send_tweet creds

Otherwise this looks OK!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.