Does Ruby have a method similar to Emacs align-regexp?
2014-09-05 10:43
239 查看
Looking for a Ruby method to operate on an array of strings and align them according to a regex given as as an argument. Emacs has a defun called align-regexp that does this interactively when operating on a region of a buffer.
Here is a portion of the emacs doc string for align-regexp.
For example, let's say you had a list of phone numbers, and wanted to align them so that the opening parentheses would line up:
There is no predefined rule to handle this, but you could easily do it using a REGEXP like "(". All you would have to do is to mark the region, call `align-regexp' and type in that regular expression. Here is the result:
Here is a portion of the emacs doc string for align-regexp.
For example, let's say you had a list of phone numbers, and wanted to align them so that the opening parentheses would line up:
Fred (123) 456-7890 Alice (123) 456-7890 Mary-Anne (123) 456-7890 Joe (123) 456-7890
There is no predefined rule to handle this, but you could easily do it using a REGEXP like "(". All you would have to do is to mark the region, call `align-regexp' and type in that regular expression. Here is the result:
Fred (123) 456-7890 Alice (123) 456-7890 Mary-Anne (123) 456-7890 Joe (123) 456-7890
lines = [ 'Fred (123) 456-7890', 'Alice (123) 456-7890', 'Mary-Anne (123) 456-7890', 'Joe (123) 456-7890', ] rows = lines.map { |line| line.partition('(') } pos = rows.map { |row| row[0].size }.max puts rows.map { |row| row[0] = row[0].ljust(pos); row.join }output:
Fred (123) 456-7890 Alice (123) 456-7890 Mary-Anne (123) 456-7890 Joe (123) 456-7890
相关文章推荐
- nodejs gyp WARN EACCES user "root" does not have permission to access the dev dir
- The current identity (NT AUTHORITY/NETWORK SERVICE) does not have write access to 'C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319
- 使用Storyboard时出现以下警告:Scene is unreachable due to lack of entry points and does not have an identifier
- Mac Os, Android Studio 升级提示:Android Studio does not have write access to /Applications/Android Studi
- [magento]Warning: Your Magento folder does not have sufficient write permissions. If you wish to pro
- cannot add a child that doesnot have a YogoNode to a parent without a measure function!
- How to configure spring boot through annotations in order to have something similar to <jsp-config> in web.xml?
- Too many arguments to method call, expected 1, have 2?
- mac os 安装Myeclipse错误: Your system does not have sufficient memory to support MyEclipse
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ? .
- User <anonymous> does not have permission on weblogic.management.adminhome to perform lookup operati
- 'xxx_Forms' is not a valid Application Database or User 'sa' does not have sufficient permissions to check
- The current identity ( XXXX) does not have write access to ‘C:\Windows\Microsoft.NET\Framework\v4.0.
- [Ruby笔记]29. Ruby yield from method to block 从方法到块
- Warning: L6305W: Image does not have an entry point. (Not specified or not set due to multi
- LINQ to Entities does not recognize the method ToString()
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedur
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ?
- JDBC连接执行 MySQL 存储过程报权限错误:User does not have access to metadata required to determine stored procedure parameter types. If rights can not be granted,
- How to limit an array of similar hashes to those that have more than one of the same key:value pair