ruby 学习 -- string --1
# define
french_string = "il \xc3\xa9tait une fois"
long_string = <<EOF
Here is a long string
With many paragraphs
EOF puts long_string.empty?
puts long_string.include? "many" puts french_string + long_string # concatenate
hash = { key1: "val1", key2: "val2" }
string = ""
str2 = ""
hash.each{|k,v| string << k.to_s << " is " << v << "\n" }
hash.each{|k,v| str2 << "#{k}" << " is " << "#{v}" << "\n"}
puts string
puts str2 # join
data = ['', '', '']
s = ''
data.each { |x| s << x << ' and a '}
puts s # => "1 and a 2 and a 3 and a "
puts data.join(' and a ') # number
number = 5
puts "The number is #{number}." # => "The number is 5."
puts "The number is #{5}." # => "The number is 5."
puts "The number after #{number} is #{number.next}."
# => "The number after 5 is 6."
puts "The number prior to #{number} is #{number-1}."
# => "The number prior to 5 is 4."
puts "We're ##{number}!" # => "We're #5!"
puts "I've set x to #{x = 5; x += 1}."
# Escaping
puts "\#{foo}"
puts '#{foo}'
# puts "#{foo}" # error because no variable of foo defined.
template = 'Oceania has always been at war with %s.'
puts template % 'Eurasia' # => "Oceania has always been at war with Eurasia." puts 'To 2 decimal places: %.4f' % Math::PI
puts 'Zero-padded: %.3d' % Math::PI
JSP, ASP type
require 'erb'
template = ERB.new %q{Chunky <%= food %>!}
food = "bacon"
puts template.result(binding) # => "Chunky bacon!"
food = "peanut butter"
puts template.result(binding) # => "Chunky peanut butter!"
puts template.result
reverse, reverse!, split
reverse 和 reverse! 的区别:reverse 不改变 string 本身, reverse! 相当于 s=s.reverse
s = ".sdrawkcab si gnirts sihT"
puts s.reverse
puts s
puts s.reverse!
puts s.split(/(\s+)/) # ["This", " ", "string", " ", "is", " ", "backwards."]
puts s.split(/\s+/) # => ["This", "sting", "is", "backwards."]
puts s.split(' ') # => ["This", "sting", "is", "backwards."]
八进制 和 十六进制 的定义
octal = "\000\001\010\020"
octal.each_byte { |x| puts x }
#
#
#
#
hexadecimal = "\x00\x01\x10\x20"
hexadecimal.each_byte { |x| puts x }
#
#
#
#
This makes it possible to represent UTF-8 characters even when you can’t type them or display them in your terminal.
Try running this program, and then opening the generated file smiley.html in your web browser:
open('smiley.html', 'wb') do |f|
f << '<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">'
f << "\xe2\x98\xBA"
end
特殊字符:
"\a" == "\x07" # => true # ASCII 0x07 = BEL (Sound system bell)
"\b" == "\x08" # => true # ASCII 0x08 = BS (Backspace)
"\e" == "\x1b" # => true # ASCII 0x1B = ESC (Escape)
"\f" == "\x0c" # => true # ASCII 0x0C = FF (Form feed)
"\n" == "\x0a" # => true # ASCII 0x0A = LF (Newline/line feed)
"\r" == "\x0d" # => true # ASCII 0x0D = CR (Carriage return)
"\t" == "\x09" # => true # ASCII 0x09 = HT (Tab/horizontal tab)
"\v" == "\x0b" # => true # ASCII 0x0B = VT (Vertical tab)
ruby里,如果是可读的ASCII字符,即使是以十六进制表示,print出来的还是可读字符。不可读,输出的为十六进制(以\x开头)字符表示形式。
"\x10\x11\xfe\xff" # => "\u0010\u0011\xFE\xFF"
"\x48\145\x6c\x6c\157\x0a" # => "Hello\n"
为了避免混淆,统一的,单反斜线由双反斜线表示:\ => \\
"\\".size # => 1
"\\" == "\x5c" # => true
"\\n"[0] == ?\\ # => true
"\\n"[1] == ?n # => true
"\\n" =~ /\n/ # => nil
组合键的表示. =~ 是正则匹配符号。
"\C-a\C-b\C-c" # => "\u0001\u0002\u0003"
"\M-a\M-b\M-c" # => "\xE1\xE2\xE3" ?\C-a # => "\u0001"
?\M-z # => "\xFA" contains_control_chars = /[\C-a-\C-^]/
'Foobar' =~ contains_control_chars # => nil
"Foo\C-zbar" =~ contains_control_chars # => 3
def snoop_on_keylog(input)
input.each_char do |b|
case b
when ?\C-c; puts 'Control-C: stopped a process?'
when ?\C-z; puts 'Control-Z: suspended a process?'
when ?\n; puts 'Newline.'
when ?\M-x; puts 'Meta-x: using Emacs?'
end
end
end snoop_on_keylog("ls -ltR\003emacsHello\012\370rot13-other-window\012\032")
# Control-C: stopped a process?
# Newline.
# Meta-x: using Emacs?
# Newline.
# Control-Z: suspended a process?
字符串定义
puts "foo\tbar"
# foo bar
puts %{foo\tbar}
# foo bar
puts %Q{foo\tbar}
# foo bar
puts 'foo\tbar'
# foo\tbar
puts %q{foo\tbar}
# foo\tbar
统计单词个数。
class String
def word_count
frequencies = Hash.new(0)
self.downcase.scan(/(\w+([-'.]\w+)*)/) { |word, ignore| frequencies[word] += 1 }
return frequencies
end
end
puts %{"The F.B.I. fella--he's quite the man-about-town."}.word_count
# => {"f.b.i"=>1, "fella"=>1, "he's"=>1,
# "quite"=>1, "the"=>2, "man-about-town"=>1}
end with 2.9 cookbook
to be continued...
ruby 学习 -- string --1的更多相关文章
- Ruby学习笔记4: 动态web app的建立
Ruby学习笔记4: 动态web app的建立 We will first build the Categories page. This page contains topics like Art, ...
- ruby 学习笔记 1
写ruby blog 系统的记录下.也是对我学ruby的点滴记录. 先介绍下我的学习环境.系统:ubuntu12.04文档:techotopia ,ruby文档,the hard way learn ...
- Ruby学习心得之 Linux下搭建Ruby环境
作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Ruby学习心得之 Linux下搭建Ruby环境1.前言2.Linux下安装Ruby环境 一 ...
- Ruby学习之mixin
直接上代码: module Action def jump @distance = rand(4) + 2 puts "I jumped forward #{@distance} feet! ...
- ruby学习网站
Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...
- JDK源码学习--String篇(二) 关于String采用final修饰的思考
JDK源码学习String篇中,有一处错误,String类用final[不能被改变的]修饰,而我却写成静态的,感谢CTO-淼淼的指正. 风一样的码农提出的String为何采用final的设计,阅读JD ...
- 学习string,stringBuffer时遇到的问题
今天学习string和stringBuffer.了解了两者的区别,然后去看java api都有啥方法.stringBuffer类有indexOf方法,于是写了下面的代码 String str = &q ...
- ruby学习笔记(1)-puts,p,print的区别
ruby学习笔记-puts,p,print的区别 共同点:都是用来屏幕输出的. 不同点:puts 输出内容后,会自动换行(如果内容参数为空,则仅输出一个换行符号):另外如果内容参数中有转义符,输出时将 ...
- Java学习——String,StringBuffer和StringBuilder
Java学习——String,StringBuffer和StringBuilder 摘要:本文主要介绍了String字符串在内存中的存储情况,以及StringBuffer和StringBuilder的 ...
随机推荐
- GPIO初始化之PB3/PB4/PA13/PA14/PA15引脚的复用--寄存器版本
为了节省IO资源单片机会在一个IO上复用很多功能,一般的单片机用到 一个功能后就能再用两外复用的功能了,这就体现出了STM32 GPIO的强大功能了,我们用重映射的方法把其中一个外设映射到其他IO脚上 ...
- 第一个完整的cppunit单元测试程序
在极限编程中,测试程序本应该在编写主程序之前就要写好,然后将写好的类程序放在测试程序中进行测试,但考虑到项目中需求文档等并未将接口定义好,我无从开始,而且,自己对单元测试也是刚刚熟悉,需要一边写测试程 ...
- struts2标签详解
struts2标签讲解 要使用Struts2的标签,只需要在JSP页面添加如下一行定义即可:<%@ taglib prefix="s" uri="/struts-t ...
- Oracle 用户权限分配说明
一般来说,Oracle普通用户创建后,不建议分配DBA权限.那么一般分配哪些权限呢? 首先来说,一个Oracle普通用户,我们一般性的要求是: 1.能够在本用户下进行本用户相关的创建表结构,数据维 ...
- 【CentOS】Eclipse中svn插件使用
目录: 1.安装 2.使用 3.错误 1.安装 svn插件地址: Subclipse 1.6.x Update Site - http://subclipse.tigris.org/update_1. ...
- C#做音乐播放器时在自动下一曲中报异常的解决办法
---------------------- ASP.Net+Unity开发..Net培训.期待与您交流! ---------------------- 在利用Media Player做音乐播放器的时 ...
- C++ Template之函数模版
函数模版的定义: template <typename T> T const& max(const T& a,const T b) { return a > b ? ...
- 【BZOJ】【3007】拯救小云公主
思路题 我的naive的做法是二分答案+判定是否有路径可走……但是没有正确理解[走的方向任意]这句话…… 其实就是说想咋走咋走= =360°无死角乱走…… 所以其实是个平面上的问题…… 我们可以换个方 ...
- 【BZOJ】【3280】小R的烦恼
网络流/费用流 和软件开发那题基本相同,只是多加了一个“雇佣研究生”的限制:不同价格的研究生有不同的数量…… 那么只需加一个附加源点,对每一种研究生连边 S->ss 容量为l[i],费用为p[i ...
- ffmpeg iOS 编译
编译模拟器版本1 到https://github.com/yuvi/gas-preprocessor下载gas-preprocessor.p并拷贝到/usr/sbin目录中2 下载ffmpeg源码.h ...