Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created January 11, 2011 22:43
Show Gist options
  • Save beccasaurus/775308 to your computer and use it in GitHub Desktop.
Save beccasaurus/775308 to your computer and use it in GitHub Desktop.
Ruby-ifying
# Ruby syntax notes:
#
# - the last value of a method is implicitly returned. 'return' is typically only used for short-circuiting
# - attr_reader :foo is the same as def foo; @foo; end
# - get_foo and set_foo methods are very Java-esque and you should typically not name methods like that in Ruby
# - instead of @array.push(1), use the push operator. @array << 1
# - if you want to do something conditionally on one line, the conditional comes *after* the code, eg. x = 5 if @dogs.any?
# - except for single line blocks, eg. each {|x| ... }, variable names should not be abbreviated
# - parenthesis are optional and should only be used when they help make the code easier to understand (subject to debate)
#
class Dependant
attr_accessor :async, :defer
attr_reader :filename, :requires, :provides
def initialize filename
@filename = filename
@provides = []
@requires = []
end
# why do these methods exist? why not just push onto these arrays directly? @dependent.provides << value
# def provides_push val
# def requires_push val
def provides_collection
collection provides
end
def requires_collection
collection requires
end
# NOTE I would use the JSON gem to do this for you so it's rock solid
def collection which
'[' + which.map {|x| "'#{x}'" }.join(',') + ']'
end
def load_attrs
self.async = false unless async
self.defer = false unless defer
"#{async}, #{defer}"
end
def to_s
"#{filename} provides (#{provides.join(',')}), requires (#{requires.join(',')}), async = #{async} and defer = #{defer}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment