Created
May 1, 2012 03:56
-
-
Save lnznt/2564840 to your computer and use it in GitHub Desktop.
[sample] the % notation [ %s!...! ] (Ruby)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: UTF-8 -*- | |
# | |
# $ ruby -v | |
# ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] | |
# | |
x = "xyz" | |
p %s!abc! | |
p %s"abc" | |
p %s#abc# | |
p %s$abc$ | |
p %s%abc% | |
p %s&abc& | |
p %s'abc' | |
p %s(abc) | |
p %s)abc) | |
p %s*abc* | |
p %s+abc+ | |
p %s,abc, | |
p %s-abc- | |
p %s.abc. | |
p %s/abc/ | |
p %s:abc: | |
p %s;abc; | |
p %s<abc> | |
p %s=abc= | |
p %s>abc> | |
p %s?abc? | |
p %s@abc@ | |
p %s[abc] | |
p %s\abc\ | |
p %s]abc] | |
p %s^abc^ | |
p %s_abc_ | |
p %s`abc` | |
p %s{abc} | |
p %s|abc| | |
p %s}abc} | |
p %s~abc~ | |
p ( %s!abc! ) | |
p ( %s"abc" ) | |
p ( %s#abc# ) | |
p ( %s$abc$ ) | |
p ( %s%abc% ) | |
p ( %s&abc& ) | |
p ( %s'abc' ) | |
p ( %s(abc) ) | |
p ( %s)abc) ) | |
p ( %s*abc* ) | |
p ( %s+abc+ ) | |
p ( %s,abc, ) | |
p ( %s-abc- ) | |
p ( %s.abc. ) | |
p ( %s/abc/ ) | |
p ( %s:abc: ) | |
p ( %s;abc; ) | |
p ( %s<abc> ) | |
p ( %s=abc= ) | |
p ( %s>abc> ) | |
p ( %s?abc? ) | |
p ( %s@abc@ ) | |
p ( %s[abc] ) | |
p ( %s\abc\ ) | |
p ( %s]abc] ) | |
p ( %s^abc^ ) | |
p ( %s_abc_ ) | |
p ( %s`abc` ) | |
p ( %s{abc} ) | |
p ( %s|abc| ) | |
p ( %s}abc} ) | |
p ( %s~abc~ ) | |
# vi:set ts=2 sw=2 et: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: UTF-8 -*- | |
# | |
# $ ruby -v | |
# <%= RUBY_DESCRIPTION %> | |
# | |
x = "xyz" | |
<% | |
string = 'abc' | |
#string = 'abc#{x}' | |
#string = 'abc%s' | |
arg = "" | |
#arg = "% x" | |
q = "s" | |
wrap1 = -> s { s } | |
wrap2 = -> s { "( #{s} )" } | |
put_code = -> wrap do | |
(0x21..0x7e).map(&:chr) | |
.grep(/[_\W]/) | |
.map {|c| [c, {"(" => ")", | |
"<" => ">", | |
"[" => "]", | |
"{" => "}"}[c] || c] } | |
.map {|c1,c2| "\%#{q}#{c1}#{string}#{c2}#{arg}" } | |
.map(&wrap) | |
.map {|s| "p #{s}" } * "\n" | |
end | |
%> | |
<%= put_code.(wrap1) %> | |
<%= put_code.(wrap2) %> | |
# vi:set ts=2 sw=2 et: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: UTF-8 -*- | |
# | |
# $ ruby -v | |
# ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux] | |
# | |
x = "xyz" | |
p %s(symbol) #=> :symbol | |
p :"symbol" #=> :symbol | |
p :'symbol' #=> :symbol | |
p %s(#{x}) #=> :"#{x}" | |
p :"#{x}" #=> :xyz | |
p :'#{x}' #=> :"#{x}" | |
p %s(%s) #=> :"%s" | |
#p :'%s' % x # undefined method `%' for :"%s":Symbol (NoMethodError) | |
#p :('%s' % x) # Syntax Error | |
#p :"%s" % x # undefined method `%' for :"%s":Symbol (NoMethodError) | |
#p :("%s" % x) # Syntax Error | |
# vi:set ts=2 sw=2 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment