[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using hash for method parameters

aidy

5/30/2007 4:10:00 PM

#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

4 Answers

Roseanne Zhang

5/30/2007 5:18:00 PM

0

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

--
Posted via http://www.ruby-....

aidy

5/30/2007 5:30:00 PM

0

On 30 May, 18:17, Roseanne Zhang <rosea...@javaranch.com> wrote:

> Try the following:
>
> class Login
>
> def with(username, password)
> @username = username
> @password = password
> end
> end
>
>
> 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


Morton Goldberg

5/30/2007 5:39:00 PM

0

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

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

You could do it with

<code>
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\">"
</code>

but I'd go with

<code>
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\">"
</code>

as being simpler.

Regards, Morton



Mariusz Pekala

5/31/2007 10:46:00 AM

0

On 2007-05-31 02:35:08 +0900 (Thu, May), aidy.lewis@googlemail.com wrote:
> On 30 May, 18:17, Roseanne Zhang <rosea...@javaranch.com> wrote:
>
> > Try the following:
> >
> > class Login
> >
> > def with(username, password)
> > @username = username
> > @password = password
> > end
> > end
> >
> >
> > 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')
>

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.

Tedy przebiegli, zaden z nich kroków nie szczedzil
mezny ten, co uciekal, mezniejszy, co pedzil.
-- 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.