using hash for method parameters

Discussion in 'Ruby' started by aidy, May 30, 2007.

  1. aidy

    aidy Guest

    #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
    aidy, May 30, 2007
    #1
    1. Advertising

  2. 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-forum.com/.
    Roseanne Zhang, May 30, 2007
    #2
    1. Advertising

  3. aidy

    Guest

    On 30 May, 18:17, Roseanne Zhang <> 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
    , May 30, 2007
    #3
  4. 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
    Morton Goldberg, May 30, 2007
    #4
  5. --cNdxnHkX5QqsyA0e
    Content-Type: text/plain; charset=utf-8
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    On 2007-05-31 02:35:08 +0900 (Thu, May), wrote:
    > On 30 May, 18:17, Roseanne Zhang <> wrote:
    >=20
    > > Try the following:
    > >
    > > class Login
    > >
    > > def with(username, password)
    > > @username =3D username
    > > @password =3D password
    > > end
    > > end
    > >
    > >
    > > login =3D 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.
    >=20
    > So, I would like my call to contain something like this:
    >=20
    > login =3D Login.new
    > login.with:)username =3D> 'aidy', :password =3D> 'aidy1')
    >=20


    You can use something like this:

    def with params =3D {}
    params.each do |key,value|
    m =3D "#{key}=3D"
    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 =3D Login.new :username =3D> 'username', :password =3D> 'pass'

    BTW this is similiar to how ActiveRecord works.

    --=20
    No virus found in this outgoing message.
    Checked by 'grep -i virus $MESSAGE'
    Trust me.

    T=C4=99dy przebiegli, =C5=BCaden z nich krok=C3=B3w nie szcz=C4=99dzi=C5=82
    m=C4=99=C5=BCny ten, co ucieka=C5=82, m=C4=99=C5=BCniejszy, co p=C4=99dzi=
    =C5=82.
    -- 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.

    --cNdxnHkX5QqsyA0e
    Content-Type: application/pgp-signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.7-ecc0.1.6 (GNU/Linux)

    iD8DBQFGXqdJsnU0scoWZKARAjSRAJkBd6Z2o8icVk/vX6tqcX2k4r7GcgCeMJue
    QFH0+qbZtTiREJODbC3Ox80=
    =Cc00
    -----END PGP SIGNATURE-----

    --cNdxnHkX5QqsyA0e--
    Mariusz Pękala, May 31, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jerry051
    Replies:
    2
    Views:
    743
    Oliver Sturm
    Aug 2, 2005
  2. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    612
    David A. Black
    Jul 2, 2008
  4. Jian Lin
    Replies:
    3
    Views:
    125
    Robert Klemme
    May 20, 2010
  5. Intransition
    Replies:
    3
    Views:
    129
    Intransition
    Jun 18, 2010
Loading...

Share This Page