Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Ruby resource manager!  (Read 6503 times)

0 Members and 1 Guest are viewing this topic.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Ruby resource manager!
« on: February 05, 2011, 05:08:56 pm »
Well there were so many resource managers for C++ so I decided to make one for Ruby too ^^

http://www.sfml-dev.org/wiki/en/sources/ruby_resource_manager

I'll soon add my animation class too as soon as I have it fully tested.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Mon ouïe

  • Newbie
  • *
  • Posts: 27
    • View Profile
Ruby resource manager!
« Reply #1 on: February 05, 2011, 05:52:34 pm »
Using came-case for anything else than a constant in Ruby is weird. Everyone does_it_like_that. I also don't like seeing parentheses containing nothing as they are unneeded in Ruby. Same for the use of the return, which I only ever use when I want to return before the end of the method.

Code: [Select]
protected
  def initialize( aClass, aLoadMethod )


initialize is private by default, even after a call to the protected method, though there are ways to change it :
Code: [Select]
def initialize; puts "Whateverer !"; end
public :initialize


Also, protected is very rarily useful. private is more often what you want.

Code: [Select]
require './ResourceManager.rb'
(The most conventional way to call that file would be resource_manager.rb, too) Don't require like that. You're relying on the current working directory, where ResourceManager.rb may not exist or be another file. Instead, put the directory containing your files into the load path (e.g. $: << File.dirname(__FILE__)) or use Kernel#require_relative.

I'd also use Hash.new's block form to cache the objects. Here's a simplified example :
Code: [Select]

class Manager
  def initialize
   @resources = Hash.new { |h, (key, filename)| h[key] = load(filename || key.to_s) }
  end

  def [](key, filename = nil)
    @resources[[key, filename]]
  end

  private
  def load(filename)
    # Do something
  end
end

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Ruby resource manager!
« Reply #2 on: February 05, 2011, 06:21:53 pm »
Quote
(The most conventional way to call that file would be resource_manager.rb, too) Don't require like that. You're relying on the current working directory, where ResourceManager.rb may not exist or be another file. Instead, put the directory containing your files into the load path (e.g. $: << File.dirname(__FILE__)) or use Kernel#require_relative.


Erhm... It's a example, why do something like that when a simple dot fixes it? Dude.... Also the latest version of Ruby-1.9.2-p0 do not include the working directory as a search path by itself. So for an EXAMPLE I did something simple.

And my personal codeconvention, it is only part of the private interface of my code as such an user of the manager won't ever be confronted with the case of the methods or variables. Though I admit, I hate seeing all this_kind_of_naming as it is harder to read. IF ruby is supposed to be easier for the developer, why the hell did they choose a crappy convention? I have my own definition of the standard library where I've renamed the methods. Also since Ruby is Duck-typed it is also even more important to specify what is an argument and not, thus why I start my arguments with a,an,some. And parenthesis methods that do not receive arguments are to specify that this is a method and not a virtual attribute or a variable. It's for readability to the developer. Same reason for returns.

Also I see from what you want to do that you don't do things readable, I try to keep things as clear as possible and method sizes to a minimum. That's why I separated some code into a private method called loadResource.

Why I use protected instead of private is because old habbit.

And using code blocks are a big no-no. I see a lot of ruby developers using code blocks to the left and right but this ends up keeping references to objects that should have been released a long time ago. Just because a language does thing for you doesn't give you a reason to start programming sloppy.

Edit: just noticed that deleteAll is not correct, though a simple aliasing fixes that. Or correction from my side since apparently people complain.....
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

 

anything