From: Andrea Dallera Date: 2010-04-02T03:35:19+09:00 Subject: Re: Dynamic Method Calls 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 # > > 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 >