Question

What should I do when defining constants or attr_accessor symbols that are very large? For example, something like this:

ATTRIBUTES = %w(id name full_name owner private html_url description fork url forks_url keys_url collaborators_url teams_url hooks_url issue_events_url events_url assignees_url branches_url tags_url blobs_url git_tags_url git_refs_url trees_url statuses_url languages_url stargazers_url contributors_url subscribers_url subscription_url commits_url git_commits_url comments_url issue_comment_url contents_url compare_url merges_url archive_url downloads_url issues_url pulls_url milestones_url)

attr_accessor :name, :login, :full_name, :owner, :private, :html_url, :description, :fork, :url

in a class is terrible. Is this the best way? I want to know if there are other ways just to improve readability.

Was it helpful?

Solution

Change lines at certain width:

ATTRIBUTES = %w[
  id name full_name owner private html_url description fork url forks_url
  keys_url collaborators_url teams_url hooks_url issue_events_url events_url
  ...
]

or if you do not need to save lines, then putting each item on a separate line may be easy:

ATTRIBUTES = %w[
  id name
  full_name
  owner
  private
  html_url
  ...
]

or if you have time formatting, then you might want to make several columns and align them:

ATTRIBUTES = %w[
  id                 name                full_name          owner             
  private html_url   description fork    url                forks_url
  keys_url           collaborators_url   teams_url          hooks_url
  issue_events_url   events_url          ...
]

In Ruby 2.0, a new literal expression %i[...] was introduced for array of symbols:

attr_accessor *%i[
  name
  login
  full_name
  owner
  private
  ...
]

OTHER TIPS

You can use a YAML file or set a config file. Then when you initialize, you can assign values.

Example YAML:

#configuration file 
config: 

  FIRST_NAME: "Bob"
  USER_ID: "abc13324"
  LAST_NAME: "Smith"
  Etc....

Example Ruby:

class NewClass
     def initialize
         configFile = YAML.load_file("pathToYourYamlFile.yaml")
         @firstName = configFile['config']['FIRST_NAME']
         @lasttName = configFile['config']['LAST_NAME']
         @user_id = configFile['config']['USER_ID']
         etc....
     end
 end 

This is best for setting constants in your program. You can then access them from any file.

Over the years, through bouncing off various languages, I've come to use this sort of layout:

ATTRIBUTES = %w(
  archive_url assignees_url
  blobs_url branches_url

  collaborators_url comments_url commits_url compare_url 
  contents_url contributors_url

  description downloads_url
  events_url
  fork forks_url full_name
  git_commits_url git_refs_url git_tags_url
  hooks_url html_url
  id issue_comment_url issue_events_url issues_url
  keys_url
  languages_url
  merges_url milestones_url
  name
  owner
  private pulls_url

  stargazers_url statuses_url subscribers_url
  subscription_url

  tags_url teams_url trees_url
  url
)

%w(
  description
  fork full_name
  html_url
  login
  name
  owner
  private
  url
).each { |a| attr_accessor a.to_sym }

Lines are sorted alphabetically, and each individual word on the line is sorted alphabetically.

If a particular line gets too long I'll wrap it and add blank lines before and after them to delimit them visually, maintaining the sort of the lines and the words in the line.

Most editors make it easy to sort, and it's the first pass cleaning up a messy list that is a pain, afterwards it's no big deal to keep it maintained. I use vim, which has the sort command, but Sublime Text 2, which I treat as my "notepad" replacement also does it.

I do this for maintenance. I find it a lot easier to scan through a line that's sorted, even if it's not evenly spaced in columns with the other lines, than I do trying to find something that isn't in a sorted order displayed in nice 'n neat columns. Neatness counts but order keeps my head from hurting. Trying to edit a table that is in columns and having it go all to heck because I added a word gets old very quickly, so the columns got tossed out, and I just sort. I'm sure it annoys my co-workers at first when I tell them to do it this way, but they see the benefit soon enough, and it shuts me up. :-)

With Ruby v2.0's addition of %i, the second example can be changed to:

%i(
  description
  fork full_name
  html_url
  login
  name
  owner
  private
  url
).each { |s| attr_accessor s }

It's not a big change, but it's a tiny bit cleaner.

Seems to me all of these are table column names.

If yes, you don't need attr_accessor for those, rails automatically provides access to them.

You can call object.name, object.html_url and so on.

Can you confirm if this is the case

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top