Accessors how to create a new one ? 
Author Message
 Accessors how to create a new one ?

Quote:

> Hi everybody,

> Is there any possibility to create my own accessor like standard accessors

Sure. Here is one that I did a while back:

class Module
  def attr_scale_reader(*attrs)
    attrs.each { |attr|
      class_eval %{
        def #{attr}_d

        end
      }
    }
  end#attr_scale_reader
end

This works a little different than the standard attr_reader,
in that it creates a function name that is different than
the attrs argument.

For your hash_reader, you could write

class Module
  def hash_reader(*attrs)
    attrs.each { |attr|
      class_eval %{
        def #{attr}

        end
      }
    }
  end#hash_reader
end
# warning, code is not tested

I'm sure that you can create the writer and accessor
functions from this example.

Cheers
Jim

Quote:
> sample

> ...
> hash_accessor :attr1
> ...

> should create methods

> def attr1

> end

> def attr1=(value)

> end

> is there any clue ?

> Best regards,

> Henry Ghusakovsky
> mail: H.Ghusakovsky(&)sam-solutions.net
> ICQ# 34523185

--
Jim Freeze
If only I had something clever to say for my comment...
~


Sun, 07 Nov 2004 20:21:27 GMT  
 Accessors how to create a new one ?
Hi everybody,

Is there any possibility to create my own accessor like standard accessors

sample

..
hash_accessor :attr1
..

should create methods

def attr1

end

def attr1=(value)

end

is there any clue ?

Best regards,

Henry Ghusakovsky
mail: H.Ghusakovsky(&)sam-solutions.net
ICQ# 34523185



Sun, 07 Nov 2004 20:21:27 GMT  
 Accessors how to create a new one ?

Quote:

> > Hi everybody,

> > Is there any possibility to create my own accessor like standard
accessors

> Sure. Here is one that I did a while back:

> class Module
>   def attr_scale_reader(*attrs)
>     attrs.each { |attr|
>       class_eval %{
>         def #{attr}_d

>         end
>       }
>     }
>   end#attr_scale_reader
> end

> This works a little different than the standard attr_reader,
> in that it creates a function name that is different than
> the attrs argument.

> For your hash_reader, you could write

> class Module
>   def hash_reader(*attrs)
>     attrs.each { |attr|
>       class_eval %{
>         def #{attr}

>         end
>       }
>     }
>   end#hash_reader
> end
> # warning, code is not tested

> I'm sure that you can create the writer and accessor
> functions from this example.

> Cheers
> Jim

thanks a lot

Best regards,

Henry Ghusakovsky
mail: H.Ghusakovsky(&)sam-solutions.net
ICQ# 34523185



Sun, 07 Nov 2004 21:28:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Creating new bitmap from part of existing one

2. Creating external accessors for class constants

3. how to create a new application with a dictionnary already created

4. GNU ST: getting user-created classes to create new instances

5. How to replace one or two words with one word with one line of awk code

6. Creating resources in files without one

7. create:group on one TAB

8. Using ABCFree To Create One Instance of App

9. creating a dir tree in one pass?

10. How to create one hash from two arrays

11. How does one create multidimensional array constants?

12. regexps: testing and creating MatchObjects in one fell swoop

 

 
Powered by phpBB® Forum Software