# How to create a product instance?

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039>\
**Category:** Ruby\
**Created:** [April 7, 2008, 9:09am UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039 "2008-04-07T09:09:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 7, 2008, 9:09am UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039/1 "2008-04-07T09:09:10Z")

</div>

Dear all,

I have created a test file like,

`
require ‘product’
product = Product.new(‘aaaa aaa’, ‘uuuu’)
 `

I have passed two strings while initializing an object

now in actual product class file

`
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
`

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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 7, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039/2 "2008-04-07T16:01:56Z")

</div>

2008/4/7, dare ruby [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> protected :name
> 
> ```
> attributes = args.last.is_a?( ::Hash ) ? args.pop : {}
> 
> # initialize attributes
> 
> @name = attributes[:name] || String.new
> 
> ```

You can as well use “” instead of String.new

> 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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 8, 2008, 5:19am UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039/3 "2008-04-08T05:19:19Z")

</div>

product = Product.new(:name =\> ‘aaaa aaa’, :foo =\> ‘uuuu’)

I have even tried by passing using a hash but its not working. but for  
me i need to pass the arguments as an array. so could you please suggest  
some way to proceed.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 8, 2008, 6:27am UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039/4 "2008-04-08T06:27:30Z")

</div>

> > product = Product.new(:name =\> ‘aaaa aaa’, :foo =\> ‘uuuu’)
> 
> Product.new({:name =\> ‘blah’})
> 
> is the same as
> 
> Product.new(:name =\> ‘blah’)

> Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
> VIDEO #3 out NOW!  
> [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

Thank you very much julian and robert its working well now. Thank you  
julian for the video files.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 8, 2008, 5:59am UTC](https://www.ruby-forum.com/t/how-to-create-a-product-instance/134039/5 "2008-04-08T05:59:10Z")

</div>

Why do you need to pass arguments as an array?

this way you mention:

> product = Product.new(:name =\> ‘aaaa aaa’, :foo =\> ‘uuuu’)

that _IS_ a hash.

Product.new({:name =\> ‘blah’})

is the same as

Product.new(:name =\> ‘blah’)

Or do you mean an array of arrays?

Julian.

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #3 out NOW!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
