第37å Rubyã®ãåå¼·
é åã¨Range
ç¹°ãè¿ããâ»ãããã¯ã§æ°å¤ã使ãã¨ã
2.upto(8) do |i| print i end ããã㯠(2..8).each do |i| print i end
ç¹°ãè¿ããâ»ãããã¯ã§æ°å¤ã使ããªãã¨ã
5.times do print 'hoge' end
ã¡ãªã¿ã«ããããã¯ã§å¼æ°ããããå ´å㯠0ããã®ã¤ã³ã¯ãªã¡ã³ã¿ã«ãªå¤ãå¾ãããã
Perlã©ã¤ã¯ã« 1..3 ã§é åãå®ç¾©ãããã¨ã
Perlã ã¨
my $c = [1..3];
ãã®ããã«é åãå®ç¾©ã§ããããRubyã§ã¯ç¡çã
Rubyã§ã¯ .. ã使ã£ãå ´åçæãããã®ã¯Rangeã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãªããã§ããã
Rangeã¯ã©ã¹ã¯eachã¨ããé
åã¨ååã®ã¡ã½ãããæã£ã¦ããã
(1..3).each do |i| print i end
ã¨ä¸è¦é åãç¹°ãè¿ãã¦ããããã«æ¸ãã¦ãã¾ããããã®æã§ç¹°ãè¿ãã¦ããã®ã¯é åã§ã¯ãªãã¦Rangeã
ã§ã¯ãã©ãããã°ã .. ã使ã£ã¦é
åãã¤ãããã®ãï¼ [1,2,3,4,5]ã¨ããããã«ä¸ã¤ãã¤æ¸ãã®ã¯æ¥µãã¦ããã©ãã§ããã
å®ã¯ç°¡å㧠Rangeãé
åã«å¤æãã¦ããã°è¯ãã to_aã¡ã½ããã使ãã°å¤æã§ããã
print (1..3).to_a.join(',') + "\n";
.to_aãã¦ããããããjoinã¡ã½ããã使ãã¦ããã®ã§ããã
ã¡ãªã¿ã«ãjoinã®å¼æ°ãæååã®å ´åãªã * ã§ãjoinã®å¹æãããã
print (1..3).to_a * ',' + "\n";
ã·ã§ã¼ãã³ã¼ãã£ã³ã°ã¨ãã®ã¨ãã¯ä¾¿å©ãªãã§ãããã
åèï¼
プログラミング言語 Ruby リファレンスマニュアル
Hashã¨Stringã¨ã·ã³ãã«
ããã·ã¥ã®ä½ãæ¹
h = { :one => 1, :two => 2 };
ãããªæã㧠{ } ã§ä½ããããã¼ã«ã¯Stringãã·ã³ãã«ãæå®ããã®ãä¸è¬çã ã¨æããã ãã©ã
以ä¸ã®ãã« ã·ã³ãã«ã§ãã¼ãæå®ããå ´åã
h['one']
㯠nil ã«ãªã£ã¡ããã
h[:one]
ã ã¨ããã¡ãã1ãè¿ã£ã¦ããããããã ã¨ããã¨ãæååã¨ã·ã³ãã«ãæ··ãããã§ã¤ãã£ã¦ããã½ã¼ã¹ã ã¨ã©ããã£ã¦ãã¼ãæå®ããã°ãããã ããï¼
Stringã®internã£ã¦ã¡ã½ããã使ãã°Stringãã·ã³ãã«ã«å¤æã§ããã¿ããã
h['one'.intern];
ã ã¨1ãè¿ã£ã¦ããã®ã§ããã§ããã¿ããã