Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7275
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: extract into a method - struggling to abstract |
| Date | 2016-07-05 23:21 +0200 |
| Message-ID | <du2mnkF7otrU1@mid.individual.net> (permalink) |
| References | <df5ec7e9-0d75-4810-9f8c-5396113e8cc4@googlegroups.com> <dtsu6eFnso6U1@mid.individual.net> <1ce04734-30d5-47d8-afaf-ae31d2537756@googlegroups.com> |
On 04.07.2016 13:27, Sayth Renshaw wrote:
> This is close.
> However not all values are integers, some are strings.
>
> Can you make templates that you can drop into the function?
Just a rough idea
data = {
'number' => :to_i,
'id' => :to_i,
'horse' => nil,
'age' => :to_i,
'sex' => :to_s,
'colour' => lambda {|x| "Color #{x}"},
}
def extract_value(path_id, data)
@doc.search('race').map do |race|
puts path_id
extracted_data =
race.search(path_id).map do |node|
{}.tap do |hash|
data.each {|d, m|
val = node[d]
hash[d.to_sym] = m ? m.to_proc[val] : val)
}
end
end
values = { race['id'].to_i => extracted_data }
ap values
# next line will prematurely return from the method,
# do you want that?
return values
end
end
There is room for optimization, i.e. avoiding repeated to_proc calls.
> PS I learnt tap from your previous post I had thought it was a typo of map but no http://ruby-doc.org/core-2.3.1/Object.html#method-i-tap :-)
:-)
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Find similar
extract into a method - struggling to abstract Sayth Renshaw <flebber.crue@gmail.com> - 2016-07-03 07:01 -0700
Re: extract into a method - struggling to abstract Robert Klemme <shortcutter@googlemail.com> - 2016-07-03 18:52 +0200
Re: extract into a method - struggling to abstract Sayth Renshaw <flebber.crue@gmail.com> - 2016-07-04 05:58 -0700
Re: extract into a method - struggling to abstract Sayth Renshaw <flebber.crue@gmail.com> - 2016-07-04 04:27 -0700
Re: extract into a method - struggling to abstract Robert Klemme <shortcutter@googlemail.com> - 2016-07-05 23:21 +0200
csiph-web