Dynamic Method Calls

Discussion in 'Ruby' started by Matt Mencel, Apr 1, 2010.

  1. Matt Mencel

    Matt Mencel Guest

    Right now I have some inefficient code that looks like this...

    LdapAccount is my model.... and record.attributes is a hash of attribute name,value pairs.


    records.each do |record|
    LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    c.buildingname = record.attributes["buildingName"].to_s if record.attributes.has_key?("buildingName")
    c.cn = record.attributes["cn"].to_s if record.attributes.has_key?("cn")
    c.department = record.attributes["department"].to_s if record.attributes.has_key?("department")
    c.displayname = record.attributes["displayName"].to_s if record.attributes.has_key?("displayName")
    c.employeetype = record.attributes["employeeType"].to_s if record.attributes.has_key?("employeeType")
    c.givenname = record.attributes["givenName"].to_s if record.attributes.has_key?("givenName")
    c.homedirectory = record.attributes["homeDirectory"].to_s if record.attributes.has_key?("homeDirectory")
    c.mail = record.attributes["mail"].to_s if record.attributes.has_key?("mail")
    end
    end


    There's probably 20 or more attributes I'll eventually be checking...I'm working with tens of thousands of records....and not all records have values for every attribute. So rather than do all the extra wasted work, I'd really like to be able to do something simpler like this...

    records.each do |record|
    LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    record.attributes.each_pair do |k,v|
    c.k = v
    end
    end
    end

    ...but I get...
    undefined method `k=' for #<LdapAccount:.....>

    Did a little research and read about the send method....so I tried this...

    c.send(k = v)


    ...but I get... (in this case the String value of v = "smtp.wiu.edu")
    ["smtp.wiu.edu"] is not a symbol


    So I'm not sure I understand how to use the send method in this case. Can anyone offer some insight? Am I headed in the right direction or is there a better way to do this.

    Thanks,
    Matt
     
    Matt Mencel, Apr 1, 2010
    #1
    1. Advertising

  2. Hei Matt,

    try c.send(k, v)
    I haven't tried that myself but it looks like what you want to accomplish.

    Bye!

    --
    Andrea Dallera
    http://github.com/bolthar/freightrain
    http://usingimho.wordpress.com

    On Thursday 01 April 2010 08:13:07 pm Matt Mencel wrote:
    > Right now I have some inefficient code that looks like this...
    >
    > LdapAccount is my model.... and record.attributes is a hash of attribute
    > name,value pairs.
    >
    >
    > records.each do |record|
    > LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    > c.buildingname = record.attributes["buildingName"].to_s if
    > record.attributes.has_key?("buildingName") c.cn =
    > record.attributes["cn"].to_s if record.attributes.has_key?("cn")
    > c.department = record.attributes["department"].to_s if
    > record.attributes.has_key?("department") c.displayname =
    > record.attributes["displayName"].to_s if
    > record.attributes.has_key?("displayName") c.employeetype =
    > record.attributes["employeeType"].to_s if
    > record.attributes.has_key?("employeeType") c.givenname =
    > record.attributes["givenName"].to_s if
    > record.attributes.has_key?("givenName") c.homedirectory =
    > record.attributes["homeDirectory"].to_s if
    > record.attributes.has_key?("homeDirectory") c.mail =
    > record.attributes["mail"].to_s if record.attributes.has_key?("mail") end
    > end
    >
    >
    > There's probably 20 or more attributes I'll eventually be checking...I'm
    > working with tens of thousands of records....and not all records have
    > values for every attribute. So rather than do all the extra wasted work,
    > I'd really like to be able to do something simpler like this...
    >
    > records.each do |record|
    > LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    > record.attributes.each_pair do |k,v|
    > c.k = v
    > end
    > end
    > end
    >
    > ...but I get...
    > undefined method `k=' for #<LdapAccount:.....>
    >
    > Did a little research and read about the send method....so I tried this...
    >
    > c.send(k = v)
    >
    >
    > ...but I get... (in this case the String value of v = "smtp.wiu.edu")
    > ["smtp.wiu.edu"] is not a symbol
    >
    >
    > So I'm not sure I understand how to use the send method in this case. Can
    > anyone offer some insight? Am I headed in the right direction or is there
    > a better way to do this.
    >
    > Thanks,
    > Matt
    >
     
    Andrea Dallera, Apr 1, 2010
    #2
    1. Advertising

  3. Whoopsie, i'm really sorry. It should be something like:

    c.send("#{k}=".to_sym, v)

    On Thursday 01 April 2010 08:13:07 pm Matt Mencel wrote:
    > Right now I have some inefficient code that looks like this...
    >
    > LdapAccount is my model.... and record.attributes is a hash of attribute
    > name,value pairs.
    >
    >
    > records.each do |record|
    > LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    > c.buildingname = record.attributes["buildingName"].to_s if
    > record.attributes.has_key?("buildingName") c.cn =
    > record.attributes["cn"].to_s if record.attributes.has_key?("cn")
    > c.department = record.attributes["department"].to_s if
    > record.attributes.has_key?("department") c.displayname =
    > record.attributes["displayName"].to_s if
    > record.attributes.has_key?("displayName") c.employeetype =
    > record.attributes["employeeType"].to_s if
    > record.attributes.has_key?("employeeType") c.givenname =
    > record.attributes["givenName"].to_s if
    > record.attributes.has_key?("givenName") c.homedirectory =
    > record.attributes["homeDirectory"].to_s if
    > record.attributes.has_key?("homeDirectory") c.mail =
    > record.attributes["mail"].to_s if record.attributes.has_key?("mail") end
    > end
    >
    >
    > There's probably 20 or more attributes I'll eventually be checking...I'm
    > working with tens of thousands of records....and not all records have
    > values for every attribute. So rather than do all the extra wasted work,
    > I'd really like to be able to do something simpler like this...
    >
    > records.each do |record|
    > LdapAccount.create:)uid => record.attributes["uid"].to_s) do |c|
    > record.attributes.each_pair do |k,v|
    > c.k = v
    > end
    > end
    > end
    >
    > ...but I get...
    > undefined method `k=' for #<LdapAccount:.....>
    >
    > Did a little research and read about the send method....so I tried this...
    >
    > c.send(k = v)
    >
    >
    > ...but I get... (in this case the String value of v = "smtp.wiu.edu")
    > ["smtp.wiu.edu"] is not a symbol
    >
    >
    > So I'm not sure I understand how to use the send method in this case. Can
    > anyone offer some insight? Am I headed in the right direction or is there
    > a better way to do this.
    >
    > Thanks,
    > Matt
    >
     
    Andrea Dallera, Apr 1, 2010
    #3
  4. Matt Mencel

    Ryan Davis Guest

    On Apr 1, 2010, at 11:35 , Andrea Dallera wrote:

    > Whoopsie, i'm really sorry. It should be something like:
    >
    > c.send("#{k}=".to_sym, v)


    to_sym is unnecessary
     
    Ryan Davis, Apr 1, 2010
    #4
    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. Honne Gowda A
    Replies:
    2
    Views:
    895
    Karl Heinz Buchegger
    Oct 31, 2003
  2. andy6
    Replies:
    2
    Views:
    773
    andy6 via DotNetMonster.com
    Jun 9, 2006
  3. Denis Palas

    Dynamic method calls

    Denis Palas, Dec 21, 2006, in forum: Java
    Replies:
    5
    Views:
    389
    Alfred
    Dec 22, 2006
  4. Richard Tobin
    Replies:
    24
    Views:
    814
  5. x1
    Replies:
    2
    Views:
    190
Loading...

Share This Page