how to create a product instance?

D

dare ruby

Dear all,

I have created a test file like,

<code>

require 'product'

product = Product.new('aaaa aaa', 'uuuu')

</code>

I have passed two strings while initializing an object

now in actual product class file

<code>

class Product

attr_accessor :name
protected :name

attr_accessor :description
protected :description

attr_accessor :categories
protected :categories

def initialize( *args )

# extract the attributes from the given arguments

attributes = args.last.is_a?( ::Hash ) ? args.pop : {}

# initialize attributes

@name = attributes[ :name ] || String.new
@description = attributes[ :description ] || String.new
@categories = attributes[ :description ] || Array.new

end


end

</code>

i have to used the arguments passed and get processed but i was not able
to retrieve string using above initialize method. please suggest a way
to proceed further.

while testing iam getting only blank space for @name, @description,
@categories.

please suggest a way so that ican read my arguments in product class
using above same initialize method.

thanks in advance

Regards,
Jose martin
 
R

Robert Klemme

2008/4/7 said:
Dear all,

I have created a test file like,

<code>

require 'product'

product = Product.new('aaaa aaa', 'uuuu')

</code>

I have passed two strings while initializing an object

now in actual product class file

<code>

class Product

attr_accessor :name
protected :name

attr_accessor :description
protected :description

attr_accessor :categories
protected :categories

def initialize( *args )

# extract the attributes from the given arguments

attributes = args.last.is_a?( ::Hash ) ? args.pop : {}

# initialize attributes

@name = attributes[ :name ] || String.new

You can as well use "" instead of String.new
@description = attributes[ :description ] || String.new
@categories = attributes[ :description ] || Array.new

end


end

</code>

i have to used the arguments passed and get processed but i was not able
to retrieve string using above initialize method. please suggest a way
to proceed further.

while testing iam getting only blank space for @name, @description,
@categories.

please suggest a way so that ican read my arguments in product class
using above same initialize method.

If you want to get values from a Hash you need to pass a Hash.

product = Product.new:)name => 'aaaa aaa', :foo => 'uuuu')

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top