instance-methods

In Ruby are there any related applications of the syntax: class << self ... end

class << self attr_accessor :n, :totalX, :totalY end The syntax above is used for defining class instance variables. But when I think about what syntax implies, it doesn't make any sense to me, so I'm wondering if this type of syntax is used for any other types of definitions. My point of confusion here is this: class << self The ap...

Fluent Interfaces - the number of objects being created

Hi guys I am in the process of creating some fluent interfaces for some simple validation stuff that I am playing around with. One thing that I have noticed is that I have a lot of different objects being created. For instance given the below statements: Check.Assertion.ForValue.That(value, "value").IsNotNull() : void Check.Assertio...

set instance variables with activescaffold

Hi All, I've recently added activescaffold to an existing rails project, and the problem I'm having is that the variable names that activescaffold is using are not the same as the ones I already have. Rather than go through my entire application and change all of the instance variable names, I'd like to be able to set the instance varia...

SELF keyword in Objective-C

In a project I'm creating, I have various classes. One of my classes has an instance of NSMutableArray that holds objects of another one of my classes. I thought I had a firm understanding on this topic, but somehow it got jumbled up in my mind again. When initializing an instance of this class, I have this initilize method: - (MMShowM...

Static variables in instance methods

Let's say I have this program: class Foo { public: unsigned int bar () { static unsigned int counter = 0; return counter++; } }; int main () { Foo a; Foo b; } (Of course this example makes no sense since I'd obviously declare "counter" as a private attribute, but it's just to illustrate the problem). ...

overriding bool() for custom class

All I want is for bool(myInstance) to return False (and for myInstance to evaluate to False when in a conditional like if/or/and. I know how to override >, <, =) I've tried this: class test: def __bool__(self): return False myInst = test() print bool(myInst) #prints "True" print myInst.__bool__() #prints "False" Any sugg...

Can a DTO have instance methods returning derived values?

Is it ever acceptable for a DTO to have instance methods which return derived values based on the DTO's data? Or should DTOs be pure data containers with no additional methods (other than getters/setters)? The purist in me says that it is far to easy for business logic to creep into such methods. However, if (for example) a DTO is sha...

Refering to javascript instance methods with a pound/hash sign

This question is similar to http://stackoverflow.com/questions/736120/why-are-methods-in-ruby-documentation-preceded-by-a-pound-sign I understand why in Ruby instance methods are proceeded with a pound sign, helping to differentiate talking about SomeClass#someMethod from SomeObject.someMethod and allowing rdoc to work. And I understand...

To static or not to static

I really like to use static methods (especially for helpers classes). But as static methods are not stubbable, eventually they are a bad practice, aren't they? So I have to choose between static methods usage convenience and testability. Is there any compromise? ...

Ruby modules and extend self

In what sort of situation is the code: module M extend self def greet puts "hello" end end more beneficial to use over say something like: module M def self.greet puts "hello" end end In the top, one is an instance method being extended, and the latter is just a class method, but when calling either method,...

Rails - Dynamically defining instance methods in a model

I'm not sure if this can even be achieved, but here goes... :) Lets assume two models, a Page model and a Field model. A Page has_many :fields and the Field model has two attributes: :name, :value What I want to achieve is in the Page model to dynamically define instance methods for each Field#name returning the Field#value. So if my ...

Defining methods on the fly in Ruby / Rails - how to set params?

I am trying to define a set of functions where I can pass in given params. for example, how do i do the following? >> get_1_type("xxx") V4_RELATIONSHIP_TYPES=[1=>2,3=>4] V4_RELATIONSHIP_TYPES.keys.each do |key| self.class.send(:define_method, "get_#{key}_type".downcase) do return GuidInfo.get_or_new(PARAMS, V4_RELATIONSHIP_TYPES[...

I know how to set class methods, but how do I set instance methods on the fly?

I asked a previous question on class methods, but I really want to understand how to do this for instance methods as well. Thanks! =) The code below sets class methods for a given array: class Testing V4_RELATIONSHIP_TYPES=[1=>2,3=>4] V4_RELATIONSHIP_TYPES.keys.each do |key| self.class.send(:define_method, "get_#{key}_type"...