using an enumerable built-in Ruby to access and manipulate nested data
2015-04-27 10:29
776 查看
array = [5, 10, [15, 20], 25, [30, 35, 40]
array#method { #block that adds 5} => [10, 15, [20, 25], 30, [35, 40, 45]You could use a recursive lambda:
add_five = lambda { |e| e.is_a?(Enumerable) ? e.map(&add_five) : e + 5 }new_array = array.map(&add_five)Adjust the
e.is_a?(Enumerable)test to match your situation,
e.is_a?(Array)would be tighter but possibly unnecessarily so.
相关文章推荐
- Python Web-第五周-Web Services and XML(Using Python to Access Web Data)
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET(ZT)
- Hack the Stack: Using Snort and Ethereal to Master the 8 Layers of an Insecure Network [ILLUSTRATED]
- using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
- Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
- Python Web-第六周-JSON and the REST Architecture(Using Python to Access Web Data)
- Using Imagecache and CCK Imagefield to Add an Image in Drupal
- Top 10 steps to optimize data access in SQL Server: Part II (Re-factor TSQL and apply best practices)
- How To Generate An XML File As A Target Datastore Using ODI In An Integration Interface ? [ID 454268
- Top 10 steps to optimize data access in SQL Server: Part III (Apply advanced indexing and denormalization)
- Applications using Launch Screen Files and targetting iOS 7.1 and earlier need to also include a Launch Image in an Asset Catalog.
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- How to access the features in an in-memory output layer using an IFeatureCursor
- using JS to control two select(html),the data can be loaded from database and XML,and show in the select
- How To Display Hierarchical Data by Using Nested Repeater Controls and Visual
- using HttpClient and sending json data to RESTful server in adroind
- How to access the features in an in-memory output layer using an IFeatureCursor
- Using jQuery To Manipulate and Filter Data
- Top 10 steps to optimize data access in SQL Server: Part V (Optimize database files and apply partitioning)
- Setting the Reply-To Header in an Email using CDONTS.NewMail Object and CDO Message