在一个ruby字符串中包含表但是或者变量。想使用不同的值替换表达式或者变量

1 类似java 或者python的printf-style方式

template = 'Oceania has always been at war with %s.'
template % 'Eurasia' # => "Oceania has always been at war with Eurasia."
template % 'Eastasia' # => "Oceania has always been at war with Eastasia."
'To 2 decimal places: %.2f' % Math::PI # => "To 2 decimal places: 3.14" 'Zero-padded: %.5d' % Math::PI # => "Zero-padded: 00003"

2 或者使用erb,因为我用的ide,所以最后一行使用的是kernel.binding ,如果你在irb中,可以只使用binding

require 'erb'
template = ERB.new %q{Chunky <%= food %>!}
food = "bacon"
puts output = template.result(Kernel.binding)

3翻转字符串

s = ".sdrawkcab si gnirts sihT"
s.reverse # => "This string is backwards."
s # => ".sdrawkcab si gnirts sihT" s.reverse! # => "This string is backwards."
s # => "This string is backwards."
s = "order. wrong the in are words These"
s.split(/(\s+)/).reverse!.join('') # => "These words are in the wrong order." s.split(/\b/).reverse!.join('') # => "These words are in the wrong. order"

(\s+)和\s+区别,(\s+)匹配的空格会包含返回列表中

"Three little words".split(/\s+/)
# => ["Three", "little", "words"]
"Three little words".split(/(\s+)/)
# => ["Three", " ", "little", " ", "words"]

ruby 字符串学习2的更多相关文章

  1. ruby 字符串学习笔记1

    1 从一种数据结构中构件字符串 hash = { key1: "val1", key2: "val2" } string = "" hash ...

  2. ruby字符串学习笔记5

    1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...

  3. ruby字符串学习笔记4

    1 单独处理字符串的字符 如果处理的是ASCII码的文档使用string#each_byte 注意 没有 string#each方法,String#each_byte 速度比 String#scan快 ...

  4. ruby 字符串学习笔记3

    ascii转字符或者字符串转ascii "a".ord # => 97 "!".ord # => 33 "\n".ord # = ...

  5. 雷林鹏分享:Ruby 字符串(String)

    Ruby 字符串(String) Ruby 中的 String 对象存储并操作一个或多个字节的任意序列,通常表示那些代表人类语言的字符. 最简单的字符串是括在单引号(单引号字符)内.在引号标记内的文本 ...

  6. Ruby字符串(1):String基本用法

    String字符串 字符串由String类提供,除了直接使用单双引号或其它字面量创建字符串,也可以使用String.new()方法来创建. a = "hello" b = Stri ...

  7. ruby 字符串常用方法学习

    引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html 1,切片:silce, [ ]-----------------[ ...

  8. ruby -- 基础学习(八)中文字符串截取的函数

    学习来源:http://www.codesky.net/article/200910/166595.html truncate(text, length = 30, truncate_string = ...

  9. Ruby Rails学习中:User 模型,验证用户数据

    用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...

随机推荐

  1. android向web提交参数的4种方式总结,附带网站案例源码

    第一种:基于http协议通过get方式提交参数 1.对多个参数的封装 public static String get_save(String name, String phone) { /** * ...

  2. 制作和unity调用动态链接库dll文件

    首先用vc建立一个dll工程 然后在里面建立一个testunity.h文件.内容如下 1 extern "C" int _declspec(dllexport)testunity( ...

  3. 如何用ABBYY把PDF转换成PPT

    在电子科技迅速发展的今天,文件格式转换并不是什么稀罕事,因为现在都是电子化办公,出现很多文件格式,但是不同的场合需要的格式不同,所以常常需要进行文件格式的转换.PDF转换成PPT也是众多文件格式转换中 ...

  4. ASP.NET MVC报错: Multiple types were found that match the controller named

    当使用ASP.NET MVC的Area功能时,出现了这样的错误: Multiple types were found that match the controller named 'Home'. T ...

  5. Java的大数操作分为BigInteger和BigDecimal

    Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类. 大数操作类代码如下: 1 pack ...

  6. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  7. 远程访问linux环境安装图形界面问题解决汇总

    本文内容转摘于其他网页,仅用于学习: 通常Linux出现 DISPLAY 尚未设置 解决方法,在root用户目录下执行#xhost +: [root@TEST144239 ~]# xhost + ac ...

  8. Bouncycastle中的RSA技术以及解决之道

    一个使用bouncycastle进行安全操作的实用类 2007-04-13 12:54 import java.io.*;import java.security.*;import java.secu ...

  9. 【linux】ps 命令详解

    [root@andon lib]# ps aux ###常用格式 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0 ...

  10. linux时间管理

    /etc/sysconfig/clock         该配置文件可用来设置用户选择何种方式显示时间.如果硬件时钟为本地时间,则UTC设为0,并且不用设置环境变量TZ.如果硬件时钟为UTC时间,则要 ...