# Wrong results using named arguments

**URL:** <https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673>\
**Category:** Ruby\
**Created:** [November 29, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673 "2006-11-29T17:21:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [November 29, 2006, 5:21pm UTC](https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673/1 "2006-11-29T17:21:45Z")

</div>

Source:

[a.rb]  
class A

```
attr_accessor :id
attr_accessor :name

def initialize(id = nil, name = nil)
	@id = id
	@name = name
end

```

end

[test.rb]  
require ‘a’

a1 = A.new(:name =\> “test”)  
a2 = A.new(:name =\> “test2”)

p a1  
p a2

Results:  
#\<A:0x28c2d80 @id={:name=\>“test”}, @name=nil\>  
#\<A:0x28c2678 @id={:name=\>“test2”}, @name=nil\>

I expected:

#\<A:0x28c2d80 @id=nil, @name=“test”\>  
#\<A:0x28c2678 @id=nil, @name=“test2”\>

So what nuby mistake am I making?

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [November 29, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673/2 "2006-11-29T18:06:01Z")

</div>

On 29/11/06, Jason Vogel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @name = name
> 
> ```
> 
> p a2  
> So what nuby mistake am I making?
> 
> Thanks,  
> Jason

Ruby really doesn’t have named arguments, rather it uses a hash to fake  
it.

Your initialize method for A should probably be written  
def initialize(args)  
@id = args[:id]  
@name = args[:name]  
end

Farrel

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [November 29, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673/3 "2006-11-29T18:36:13Z")

</div>

Jason Vogel wrote:

> @name = name  
> end  
> end
> 
> [test.rb]  
> require ‘a’
> 
> a1 = A.new(:name =\> “test”)  
> a2 = A.new(:name =\> “test2”)

As your class is written, you must provide values:

a1 = A.new(“id”,“test”)

Do you want to define a hash with key-value pairs? Do it this way:

def initialize(key,value)  
@hash = {}  
@hash[key] = value  
end

In a post like this, always say explicitly what you want to accomplish.

---

<div class="post-metadata">

**Author:** ![vinsonizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/848f3c/32.png) [@vinsonizer](https://www.ruby-forum.com/u/vinsonizer)\
**Post date:** [November 29, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/wrong-results-using-named-arguments/77673/4 "2006-11-29T18:38:13Z")

</div>

Jason Vogel:

> 

# […]

> #\<A:0x28c2678 @id={:name=\>“test2”}, @name=nil\>  
> Your initialize method takes two (optional) arguments, the first of  
> which  
> is id. In your calls to A.new, you’re passing exactly one argument,  
> which  
> is why id gets set, and name does not. More precisely,

```
A.new(:name => 'test')

```

is the same as

```
A.new({:name => 'test'})

```

so you’re passing a Hash. To achieve what you may want, do

```
def initialize(opt = {})
  @id = opt[:id]
  @name = opt[:name]
end

```

Kalman
