CGI::escapeHTML
CGI::escapeHTMLã¡ã½ããã使ãã¨ãHTMLã®ã¨ã¹ã±ã¼ããè¡ããã¾ããto_sãã¤ãã¦ããä¸ããªãã¨ã¨ã©ã¼ã«ãªã£ã¦ãã¾ãã¾ãããããããescapeHTMLã¡ã½ããã«æåå以å¤ã渡ãã¦ãã¾ãã¨ã¾ããã®ã§ãããã
çå CGI::escapeHTMLã¨CGI.escapeHTMLã¯ã©ããéãã®ã§ããããã©ã¡ãã§ãåãããã§ããããã¥ã¢ã³ã¹ã¯ã©ãç°ãªãã®ã§ãããã::ã¯ã¹ã³ã¼ãæ¼ç®åã.ã¯ã¯ã©ã¹ã¡ã½ããã¨ãããã¨ã ã¨æãã®ã§ããâ¦ãPerlãªäººã«ã¯::ã®ã»ãã親ãã¿ãããã¨ã¯æãã¾ãããã©ãâã³ãã³ã¨ããªãªã
#!ruby require "cgi" cgi = CGI.new print <<"EOD" Content-type: text/html <html> <title>Hello</title> <body> <h1>Hello</h1> <form method="post"> name: <br> <input type="text" name="name" value=""> <br> message: <br> <textarea name="message"> </textarea> <br> <input type="submit" value="send"> </form> <hr> <dl> <dt>name</dt><dd>#{CGI::escapeHTML(cgi.params["name"].to_s)}</dd> <dt>message</dt><dd>#{CGI::escapeHTML(cgi.params["message"].to_s)}</dd> </dl> </body> </html> EOD
CGI.new
CGI.newã§å¾ããªãã¸ã§ã¯ãã¯ããã·ã¥ã§ããä¸ãããããã£ã¼ã«ãã¯cgi.params[ãã£ã¼ã«ãå]ã§å¾ããã¨ãã§ãã¾ãã
以ä¸ã®CGIã§ã¯ãã¦ã¼ã¶ãå
¥åããæååãã¨ã¹ã±ã¼ãããã«åºåãã¾ãã®ã§ãXSSèå¼±æ§ãããã¾ãã
#!ruby require "cgi" cgi = CGI.new print <<"EOD" Content-type: text/html <html> <title>Hello</title> <body> <h1>Hello</h1> <form method="post"> name: <br> <input type="text" name="name" value=""> <br> message: <br> <textarea name="message"> </textarea> <br> <input type="submit" value="send"> </form> <hr> <dl> <dt>name</dt><dd>#{cgi.params["name"]}</dd> <dt>message</dt><dd>#{cgi.params["message"]}</dd> </dl> </body> </html> EOD
open("| command", "r")
openã®ç¬¬ä¸å¼æ°ã|ã§å§ããã¨ã³ãã³ããæå³ãã¾ãã第äºå¼æ°ã"r"ãªãå®è¡çµæããèªã¿è¾¼ããã¨ãã§ãã¾ãããããã¯ãã©ã¡ã¼ã¿ã¯IOãªãã¸ã§ã¯ãã§ãã
open("| dir /s/b", "r") { |file| print file.class, "\n" while line = file.gets print "-- #{line.chomp} --\n" end }
Time.now
Time.nowã¯ç¾å¨æ¥æãè¿ãã¾ããTime.newã¨ä¸åéãã§ããããã£ã¦ãããã¨ã¯åãã§ãã
print Time.now
ç°å¢å¤æ°RI
riã®ãªãã·ã§ã³ã¯ç°å¢å¤æ°RIã§ã»ããã§ãã
set RI=-T -w 128
è¡é ã«classãããè¡ãæ¤ç´¢
æ¨æºå ¥åã®åè¡ã«å¯¾ãã¦å¦çãè¡ãã¨ããã®ã¯åºæ¬çãªãã¿ã¼ã³ã§ããè¡é ã«classãããè¡ãæ¤ç´¢ãã¦ã¿ã¾ããã
while line = gets if line =~ /^class/ print $_ end end
getsã¯$_ã«ä»£å ¥
getsã®æ»ãå¤ã¯ç¹æ®å¤æ°$_ã«ãä»£å ¥ããã¾ããgetsã®æ»ãå¤ã¯å ¥åçµç«¯ã§nilã«ãªãã¾ãããã以ä¸ã®ããã°ã©ã ã§æ¨æºå ¥åããã¹ã¦æ¨æºåºåã«ã³ãã¼ãããã¨ã«ãªãã¾ãã
while gets print $_ end
ri -T -w 128
riã®åºåããã¼ã¸ã£ãéããã«è¡¨ç¤ºããã«ã¯-Tãªãã·ã§ã³ãã¤ãã¾ãã
ç»é¢å¹
ã128æ¡ã«ããã«ã¯-w 128ãªãã·ã§ã³ãã¤ãã¾ãã
ri -T -w 128 IO#gets
loopã»ã
Rubyã§ã¯ãtimes, loop, eachã¯ã¡ã½ããã§ãfor, while, untilã¯ã¡ã½ããã§ã¯ããã¾ããã
loop { print "Infinite loop!\n" }
each
timesã¯Integerã®ã¡ã½ããã§ããããeachã¯Array(ãªã©)ã®ã¡ã½ããã§ãã
range = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] range.each { |i| print "i = #{i}\n" }
until
whileã«å¯¾ããuntilã¯ãifã«å¯¾ããunlessã®ãããªãã®ã§ããã
i = 0 until 10 <= i print "i = #{i}\n" i += 1 end
forã¨é å
é åãæå®ãã¦foræãã¾ãããã¨ãã§ãã¾ãã
range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for i in range print "i = #{i}\n" end
for
ç¯å²ãæå®ãã¦foræãã¾ããã¾ããx..yã¯[x, y]ã§ãx...yã¯[x, y)ã§ããforã¯ã¡ã½ããã§ã¯ããã¾ããã
for i in 0..9 print "i = #{i}\n" end for i in 0...10 print "i = #{i}\n" end
ri -c
ri -cã§ãriãç¥ã£ã¦ããã¯ã©ã¹ã¨ã¢ã¸ã¥ã¼ã«ã®ä¸è¦§ã表示ããã¾ãã
timesã¡ã½ãã
timesã¡ã½ããã«ããç¹°ãè¿ãã§ãã
10.times {|i| print "i = #{i}\n" }
ä»£å ¥ã®æç¡ã¨ã¡ã½ããå¼ã³åºã
Rubyã§ã¯ãå¤æ°ã¨ã¡ã½ããã®åºå¥ãä»ããªãã¨ãã以åã«ä»£å ¥ããããã©ããã調ã¹ã¦å¤æãã¾ãã
def f "function\n" end print f # => function f = "var\n" print f # => var print f() # => function
intern
Rubyã§ã¯ã:ãã¤ããã¨ã·ã³ãã«ãå¾ããã¾ããåãã·ã³ãã«ã¯åããªãã¸ã§ã¯ãã§ãã
print :Rubyco, "\n" # => Rubyco if :Rubyco == :Rubyco print "1 Same\n" # => 1 Same end if :Rubyco.object_id == :Rubyco.object_id print "2 Same\n" # => 2 Same end
追è¨: åããªãã¸ã§ã¯ããã©ãããæ¯è¼ããã®ã« == ã¯ä¸é©åã§ãããObject#equal?ã使ãã¹ãã§ãããã
print :Rubyco, "\n" # => Rubyco if :Rubyco.equal?(:Rubyco) print "1 Same\n" # => 1 Same end if :Rubyco.object_id == :Rubyco.object_id print "2 Same\n" # => 2 Same end
追è¨ï¼id:nekokakãããããããã¯ãã¼ã¯ã§object_idã®æ¯è¼ã¯==ã§ããã®ï¼ã¨ææãããã¾ãããããã¨ãããã¨â¦ããã§ã¯:Rubycoã¨:Rubycoã®object_idãåãå¤ãã©ãããè¦ã¦ããã ãã§ãããã(object_idã®identityã§ã¯ãªãequalityã調ã¹ã¦ãã)ã®ã§ã==ã§ããã®ã§ã¯ãªããã¨æãã¾ãã
print :Rubyco, "\n" # => Rubyco if :Rubyco.equal?(:Rubyco) print "1 Same\n" # => 1 Same end if :Rubyco.object_id.equal?(:Rubyco.object_id) print "2 Same\n" # => 2 Same end
ã¡ãªã¿ã«ãRubyã§ã¯ãã¹ã¦ããªãã¸ã§ã¯ããªã®ã§ãobject_idããªãã¸ã§ã¯ãã§ãã以ä¸å®é¨ã§ãã
p :Rubyco.object_id.class #=> Fixnum
idã¨object_id
Rubyã§ã"string".idã使ããã¨ããããwarning: Object#id will be deprecated; use Object#object_idã¨ããè¦åãåºã¾ããã®ã§object_idã使ãã¾ãã
æååãªãã©ã«ã¯ãã³ã¼ãä¸ã«åºã¦ããã¨æ°ãã«newããããã§ããobject_idãç°ãªãã®ã§ãããã¾ãã
if "string".object_id != "string".object_id print "Diff\n" end # => Diff
ã¤ã³ãã³ãä»ããã¢ããã¥ã¡ã³ã
Rubyã§ã¯ã<<ã®å¾ã«-ãã¤ããã¨ãçµç«¯æååã«ã¤ã³ãã³ããã¤ãããã¨ãã§ãã¾ãã
print <<"EOD" Hello, hello, hello. Hello, hello, hello. Hello, hello, hello. EOD print <<-"EOD" Hello, hello, hello. Hello, hello, hello. Hello, hello, hello. EOD