# Hash map during the creation of a new class instance?

**URL:** <https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800>\
**Category:** Ruby\
**Created:** [October 20, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800 "2007-10-20T13:25:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800/1 "2007-10-20T13:25:34Z")

</div>

Hi,

Is it possible to use a hash map during the creation of a new class  
instance?

E.g.

Test\_Case\_1.new(test = {:name =\> ‘case\_1’})

If so, what would the syntax be in the class initialisation method

class Test\_Case\_1  
def initialize(test[:name]) ?

Thank You

Aidy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800/2 "2007-10-20T13:50:43Z")

</div>

On 20.10.2007 13:22, removed\_email\_address@domain.invalid wrote:

> Is it possible to use a hash map during the creation of a new class  
> instance?
> 
> E.g.
> 
> Test\_Case\_1.new(test = {:name =\> ‘case\_1’})

And what should this do?

> If so, what would the syntax be in the class initialisation method
> 
> class Test\_Case\_1  
> def initialize(test[:name]) ?

As with every method you can this syntax for invoking methods:

irb(main):001:0\> def foo(h) h end  
=\> nil  
irb(main):002:0\> foo(:name =\> “hello”, :age =\> 22)  
=\> {:name=\>“hello”, :age=\>22}

It depends on what you want to do how you would implement initialize  
here.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800/3 "2007-10-20T14:15:52Z")

</div>

Hi Robert

On 20 Oct, 04:46, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Test\_Case\_1.new(test = {:name =\> ‘case\_1’})
> 
> And what should this do?

I was thiking of passing meaningful data into a test case class I have

class Test\_Case\_1  
def initialize() #not sure as yet as to what to enter here

```
log.detail_test_case(test[:name], .....])

```

I dont want to hard code the data into the class, and was thinking of  
passing test data during initialisation. I could then initialise the  
class x number of times with different test data.

> irb(main):001:0\> def foo(h) h end  
> =\> nil  
> irb(main):002:0\> foo(:name =\> “hello”, :age =\> 22)  
> =\> {:name=\>“hello”, :age=\>22}

So, I can put a placeholder in this method

def initialize(x)

> It depends on what you want to do how you would implement initialize here.

My aim is put put test data somewhere meaningful, but for the tests  
to be easily read.

log.detail\_test\_case(test[:name], test[:description])  
browser.goto(test[:url])

> Kind regards
> 
> ```
> robert
> 
> ```

Your advice is highly appreciated.

aidy

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 20, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/hash-map-during-the-creation-of-a-new-class-instance/117800/4 "2007-10-20T14:25:49Z")

</div>

On 20.10.2007 14:11, removed\_email\_address@domain.invalid wrote:

> > 
> 
> > > def initialize(test[:name]) ?
> 
> def initialize(x)

“x” is called a “parameter”.

> **[Parameter (computer programming)](https://en.wikipedia.org/wiki/Parameter_%28computer_science%29)**
>
> In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine.\[a\] These pieces of data are the values of the arguments (often called actual arguments or actual parameters) with which the subroutine is going to be called/invoked. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its argu Unlike argu...

In your case you can simply do

def initialize(test)  
log.detail\_test\_case(test[:name], …])  
end

And then

tc = TestCase1.new(:name =\> “test 1”, :run =\> 1)

> > ```
> > robert
> > 
> > ```
> 
> Your advice is highly appreciated.
> 
> aidy

Cheers

robert
