# Using hash for method parameters

**URL:** <https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518>\
**Category:** Ruby\
**Created:** [May 30, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518 "2007-05-30T18:10:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 30, 2007, 6:10pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/1 "2007-05-30T18:10:53Z")

</div>

#hi,

#I have a login class

class Login

def with(:username, :password)  
login.username.set(:username)  
login.password.set(:password)  
end  
end

#now in my invocation, I would like to do something like this,

login = Login.new  
login.with(:username =\> ‘aidy’, :password =\> ‘aidy1’)

#but I am ensure of my syntax

#regards

#aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 30, 2007, 7:36pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/2 "2007-05-30T19:36:04Z")

</div>

On 30 May, 18:17, Roseanne Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> login = Login.new  
> login.with(“abc”, “def”)  
> p login

Thanks for the post, but I am trying to explicitly name the parameters  
in the call, by attempting to use symbols and a hash, to make things a  
little more readable.

So, I would like my call to contain something like this:

login = Login.new  
login.with(:username =\> ‘aidy’, :password =\> ‘aidy1’)

cheers

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 30, 2007, 7:40pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/3 "2007-05-30T19:40:05Z")

</div>

On May 30, 2007, at 12:10 PM, aidy wrote:

> end
> 
> #now in my invocation, I would like to do something like this,
> 
> login = Login.new  
> login.with(:username =\> ‘aidy’, :password =\> ‘aidy1’)
> 
> #but I am ensure of my syntax

You could do it with

`
class Login
    def with(params)
       @username = params[:username]
       @password = params[:password]
    end
end
login = Login.new
login.with(:username => ‘aidy’, :password => ‘aidy1’)
login.inspect # => “#<Login:0x24090 @password=“aidy1”, @username=
“aidy”>”
`

but I’d go with

`
class Login
    def initialize(params)
       @username = params[:username]
       @password = params[:password]
    end
end
login = Login.new(:username => ‘aidy’, :password => ‘aidy1’)
login.inspect # => “#<Login:0x23d48 @password=“aidy1”, @username=
“aidy”>”
`

as being simpler.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 30, 2007, 7:17pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/4 "2007-05-30T19:17:40Z")

</div>

aidy wrote:

> #hi,
> 
> #but I am ensure of my syntax
> 
> #regards
> 
> #aidy

Are you creating your new language?

Try the following:

class Login

def with(username, password)  
@username = username  
@password = password  
end  
end

#now in my invocation, I would like to do something like this,

login = Login.new  
login.with(“abc”, “def”)  
p login

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 31, 2007, 12:46pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/5 "2007-05-31T12:46:53Z")

</div>

On 2007-05-31 02:35:08 +0900 (Thu, May), removed\_email\_address@domain.invalid  
wrote:

> > end  
> > So, I would like my call to contain something like this:
> 
> login = Login.new  
> login.with(:username =\> ‘aidy’, :password =\> ‘aidy1’)

You can use something like this:

def with params = {}  
params.each do |key,value|  
m = “#{key}=”  
self.send(m, value) if self.respond\_to?(m)  
end  
end

but you should build-in additional checks which will filter out possibly  
unsafe assignments, which you would not want here.

Another thing, you may want to call it ‘initialize’, to be able to use:

login = Login.new :username =\> ‘username’, :password =\> ‘pass’

BTW this is similiar to how ActiveRecord works.

–  
No virus found in this outgoing message.  
Checked by ‘grep -i virus $MESSAGE’  
Trust me.

TÄ™dy przebiegli, Å¼aden z nich krokÃ³w nie szczÄ™dziÅ‚  
mÄ™Å¼ny ten, co uciekaÅ‚, mÄ™Å¼niejszy, co pÄ™dziÅ‚.  
– Homer, Iliada.

Ceterum censeo Internet Explorer esse delendam.

I have tried to Google this question, but Google said:  
“Application error, Rails application failed to start properly”

() The ASCII Ribbon Campaign - against HTML Email,  
/\ vCards, and proprietary formats.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [May 30, 2007, 8:26pm UTC](https://www.ruby-forum.com/t/using-hash-for-method-parameters/105518/6 "2007-05-30T20:26:10Z")

</div>

> > login.with(“abc”, “def”)
> 
> cheers
> 
> aidy

You could use something like:

class Login  
def with(params)  
@username = params[:username]  
@password = params[:password]  
end  
end

login = Login.new  
login.with({:username =\> ‘aidy’, :password =\> ‘aidy1’})

Haven’t tested this but it gives the general idea.

Gary Thomas
