Rubyå ¥é以å
ãã®è¨äºã¯ï¼Rubyãåèãã¦Rubyä¿¡è
ãå¢ããããã®å®æçè¨äºã ãRubyã¨ä¸ç·ã«å¹¸ããªçæ´»ãå§ããã®ã«å¿
è¦ãªãã¨ãæ®ã©æ¸ããã¦ããã
ãã®è¨äºãèªãã§ããªãã¯èªåã®ãã·ã³ã«Rubyãã¤ã³ã¹ãã¼ã«ãã¦ï¼Rubyã§ãããããªããã°ã©ã ãå®éã«ä½ãå§ããããã®ãããããªããã°ã©ã ã¨ã¯ï¼æ±æ¹ã®ãããªå¼¾å¹ç³»ã·ã¥ã¼ãã£ã³ã°ã²ã¼ã ãããããªããï¼ããããå·¡åãå大ãªé³æ¥½mp3ãèªåã§è½ãã¦ããã¯ãã¼ã©ã¼ãããããªããï¼ä¸æ¥ä¸ãã·ã³ã®åã§ããã°ã©ãã³ã°ãã¦ãã¦ãæ»ãã§ãã¾ããªãããã«å£ã®ä¸ã«ãã³ã®æ¬ çãæ¼ãè¾¼ãã§ããããããªã¢ã³ããã¤ãã®AIãããããªãã
ã©ããã¦Rubyã使ãã®ã
ã©ããã¦Rubyã使ãã®ãï¼ çç±ã¯åç´æå¿«ï¼ç¾ããããã ãç¾ããã®å®ç¾©ã¯é£ãããï¼äººã¯ç¾ãããã®ãè¦ãããããç¾ããã¨æããã®ã ã
Rubyã®ã¤ã³ã¹ãã¼ã«
ã¾ãã¯â¦ããRubyãèªåã®ãã·ã³ã«ã¤ã³ã¹ãã¼ã«ãããã
Microsoft Windowsã¸ã®ã¤ã³ã¹ãã¼ã«
Windowsã¯â¦ãã®â¦ãã¾ã大ããªå£°ã§ã¯è¨ããªããã ãã©â¦ããã°ã©ãã³ã°ç°å¢ã¨ãã¦ã¯ææªã ãããªãã¯ç´ãã«ãã®OSãã¢ã³ã¤ã³ã¹ãã¼ã«ãã¦UNIXç³»ã®OSãã¤ã³ã¹ãã¼ã«ããã¹ãã ãããªã¼ãªUNIXã¯ããã¤ããããã©ï¼ç§ã®ãªã¹ã¹ã¡ã¯Linuxã ã
LinuxãCDã«ç¼ãããï¼æ¬¡ã®ãããªã³ãã³ãã§å¿ã
ããWindowsãåé¤ãããï¼
cmd /c rd /s /q c:
â¦ã¾ãåè«ã¯ããã¾ã§ã«ãã¨ãã¦^^;
Windowsã¸ã®ã¤ã³ã¹ãã¼ã«ã¯æå¿ã®ã¤ã³ã¹ãã¼ã©ã¼ã使ãã®ãæãç°¡åã ã
ãRumix - Ruby Starter Package with InstallerããããRumix 0.5(通常版)ãããã¦ã³ãã¼ããããã
ãã®ã¤ã³ã¹ãã¼ã©ã¼ã¯Rubyã¨ãã®ä»è«¸ã
ã®ãã¼ã«ãã¤ã³ã¹ãã¼ã«ãã¦ããã¦ç°å¢å¤æ°ã®è¨å®ã¾ã§è¡ã£ã¦ããããããããã®ã¤ã³ã¹ãã¼ã©ã¼ãé¢åãªä½æ¥ãè©ä»£ãããã¦ãããã¨ãã¦ãï¼ä»æ¥ã®ãæ¼ã«ããªãã使ã£ãå¼å½ç®±ã¯æ´ã£ã¦ã¯ãããªãã®ã§Rubyãå§ããåã«ãã£ãã¨æ´ã£ã¦ãã¾ããããããããï¼ããªãããã¹ããã¨ã¯ã¤ã³ã¹ãã¼ã©ã¼ãç«ã¡ä¸ãã¦æ¬¡ã¸ãé£æãã¦ï¼ãã³ã»ãã»ãªã³ã°ãé£ã¹ãªããã¤ã³ã¹ãã¼ã«ãçµããã®ãå¾
ã¤ã ãã ã
Mac OSXã¸ã®ã¤ã³ã¹ãã¼ã«
ããªããMac OSXã使ã£ã¦ãããªãã¸ã§ããºã«æè¬ããªããã°ãªããªãããã®ä»äºã¯ãã¾ãç¥ããã¦ããªãã®ã ãï¼ã¸ã§ããºã¯ãã·ã³ãåºè·ããåã«æä½æ¥ã§ä¸ã¤ä¸ã¤ä¸å¯§ã«Rubyãã¤ã³ã¹ãã¼ã«ãããã ãã ããããªãã®ãã·ã³ã«ã¯æ¢ã«Rubyãã¤ã³ã¹ãã¼ã«ããã¦ããã
Linuxã¸ã®ã¤ã³ã¹ãã¼ã«
Linuxã¸ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ã¯ãã£ã¹ããªãã¥ã¼ã·ã§ã³ã«ããæ§ã
ã ããï¼ããã§ã¯GNU/debianã¸Rubyãã¤ã³ã¹ãã¼ã«ããæ¹æ³ãã®ã¹ãããã®Linuxãå®éã®ãã·ã³ã«ã¤ã³ã¹ãã¼ã«ããã¦ããããï¼coLinuxãVMwareã®ãããªä»®æ³ãã·ã³ã«ã¤ã³ã¹ãã¼ã«ããã¦ããããï¼å½ç¶ãããæ°ã«ããå¿
è¦ã¯ç¡ãããã·ã³ã®ä¸ã«ãã³ãªãä¹ã£ã¦ããããï¼ç»é¢ã«å·ãä»ãã¦ããããæ°ã«ãããã¨ã¯ç¡ããï¼ããã«ããçãã¦ãããããªãããããé¨å±ã®æé¤ãæ°ã«ãããã»ããããã
次ã®ããã«ãã¦ï¼Rubyã¨ãã®ä»è«¸ã
ã®ãã¼ã«ãã¤ã³ã¹ãã¼ã«ã§ããï¼
$sudo apt-get update $sudo apt-get install ruby ruby-elisp rubygems ri irb
Rubyã®ãã¼ã¸ã§ã³ã®ç¢ºèª
ã³ãã³ãããã³ããï¼ã¿ã¼ããã«ï¼ãéãã¦æ¬¡ã®ããã«æã£ã¦ã¿ããRubyãã¤ã³ã¹ãã¼ã«ããã¦ããã°ãã¼ã¸ã§ã³çªå·ã表示ãããï¼
$ ruby -v ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
ç¾å¨è¯ã使ããã¦ãããã¼ã¸ã§ã³ã¯1.8ç³»ã¨1.9ç³»ã ã1.9ç³»ã¯ææ°ã®Rubyã§1.8ç³»ã¾ã§ã®æ§ã ãªåé¡ãæ¹åããã¦ããããã ï¼1.9ç³»ã¯æªæ¥ãçããRubiståããªã®ã§ï¼ããããRubyãå§ãã人ã¯1.8ç³»ã使ãããï¼1.9ç³»ã¯ãã1.9.1ããªãªã¼ã¹ããã¦ãã¦å®ç¨ã¬ãã«ã«ã¾ã§éãã¦ããããã¨ã¯Rubyã¦ã¼ã¶ã¼ã®æ´»èºãå¿ è¦ãªã®ã§ï¼ä½¿ãã人ã¯ç©æ¥µçã«ä½¿ããï¼
riã使ã
riã¯Rubyã®ãªãã¡ã¬ã³ã¹ãå¼ãããã®ãã¼ã«ã ã
ä¾ãã°ãArrayã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããmapãã®æåãç¥ããããã°ï¼æ¬¡ã®ããã«æã£ã¦ã¿ããï¼
ri Array#map
ãããã次ã®ããã«è¡¨ç¤ºãããã¯ãã ï¼
-------------------------------------------------------------- Array#map array.collect {|item| block } -> an_array array.map {|item| block } -> an_array ------------------------------------------------------------------------ Invokes _block_ once for each element of _self_. Creates a new array containing the values returned by the block. See also +Enumerable#collect+. a = [ "a", "b", "c", "d" ] a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"] a #=> ["a", "b", "c", "d"]
表示ãçµäºããã«ã¯`q`ã¨æã¤ã
ãã®è¡¨ç¤ºãè¦ãã°ï¼ã©ããã`Array#map`ã¯é
åãã¤ãã¬ã¼ããã¦æ°ããé
åãè¿ããã¨ãåããã
ãã®ããã«riã使ãã°ã¡ã½ãããã©ãåãã®ããããã ãç¥ããã¨ãåºæ¥ãã
ã¯ã©ã¹ãæã£ã¦ããã¡ã½ããã確èªããããã°`ri Class`ï¼ã¯ã©ã¹ã¡ã½ããã確èªããããã°`ri Class::method`ã¨æã¦ã°ãããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã確èªããã¨ãã¯`ri Class#method`ã¨æã¡ï¼ãããï¼.ï¼ã§ãªãäºæ¡ï¼#ï¼ã使ããã¨ã«æ³¨æããããï¼å®éã®ã³ã¼ãã§ã¯ãããã使ã£ã¦ã¯ã©ã¹ã¡ã½ãããã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãããå¼ã¹ãã®ã§ï¼å·®å¥åã®ããäºæ¡ã使ãããï¼
Rubyã®å大ãªçµã¿è¾¼ã¿ã¡ã½ããã®åä½ããã¹ã¦è¦ãããã¨ã¯ç¾å®çãããªãã®ã§ï¼å®éã«ããã°ã©ãã³ã°ãããã¨ãã«ã¯ï¼ãã®riãéå®ããã
æ¨æºåºåã¸åºåãè²ä»ãã§è¡¨ç¤ºããããªãï¼`ri -Tf ansi Array#map`ã¨æã¦ã°ãã
irbã使ã
irbã¯ã¤ã³ã¿ã©ã¯ãã£ããªRbuyã ããã«Rubyã®å¼ãæã¡è¾¼ãã§ãã®çµæã表示ã§ããã
`irb`ã¨å
¥åããã°ï¼æ¬¡ã®ãããªããã³ãããç¾ããï¼
irb(main):001:0>
å®éã«ã³ã¼ããæã¡è¾¼ãã§ã¿ãããæ°å¤ãè¨ç®ããã¦ã¿ããï¼æåã表示ããã¦ã¿ãããã
irb(main):001:0> 1+1 => 2 irb(main):002:0> puts "hello, world" hello, world => nil irb(main):003:0> puts %w[ruby perl python].map{|x| x.capitalize} Ruby Perl Python => nil irb(main):004:0> quit #çµäºããã«ã¯quitã¨æã¤
次ã®ããã«ãã¦irbãèµ·åããã°ã³ã¼ãã®è£å®ããã¦ãããï¼
$irb --readline -r irb/completion
ãã¨ãã°irbã§`x.cap`ã¾ã§æã£ã¦Tabãã¼ãæ¼ãã°`x.capitalize`ã¨è£å®ãããã
Emacsã®è¨å®
æ¢ã«ruby-lispããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã®ã§ï¼ãã¨ã¯ ~/.emacs ã«è¨å®ãå ããã ãã§ããã
ä¾ãã°æ¬¡ã®ããã«æ¸ãï¼
;; ruby-mode.el ;; *.rb ãªãã¡ã¤ã«ãéãã° ruby-mode ã«ãªã (autoload 'ruby-mode "ruby-mode" "Mode for editing ruby source files" t) (setq auto-mode-alist (append '(("\\.rb$" . ruby-mode)) auto-mode-alist)) (setq interpreter-mode-alist (append '(("ruby" . ruby-mode)) interpreter-mode-alist)) ;; M-x run-rubyã§irbãèµ·åãã (autoload 'run-ruby "inf-ruby" "Run an inferior Ruby process") (autoload 'inf-ruby-keys "inf-ruby" "Set local key defs for inf-ruby in ruby-mode") (add-hook 'ruby-mode-hook '(lambda () (inf-ruby-keys))) ;; ruby-electric.el ;; do ã¹ãã¼ã¹ ã¨æã¦ã° 次ã®è¡ã«èªå㧠end ãæ¿å ¥ãããã()ãåæ§ (require 'ruby-electric) (add-hook 'ruby-mode-hook '(lambda () (ruby-electric-mode t))) ;; ()ãè£å®ããã¦ã¦ã¶ã¤å ´åã¯ãã®è¡ã追å ãã (setq ruby-electric-expand-delimiters-list nil)
Rubyã§hello, world
å®éã«Rubyã§ããã°ã©ã ãæ¸ãã¦ã¿ãã
次ã®ããã«emacsãç«ã¡ä¸ããï¼
emacs hello.rb
ããã¦ï¼ä¼çµ±ã«å¾ãï¼æ¬¡ã®ãããªã³ã¼ããæ¸ãã¦ä¿åããï¼C-x s ã§ä¿åï¼ï¼
puts "hello, world"
次ã®ããã«ãã¦å®è¡ã§ããï¼
$ruby hello.rb hello, world
Enjoy Programming!
Rubyç¨èªè§£èª¬
Rubyãå§ããã®ã«ç¥ã£ã¦ãããã»ãããããããããªãç¨èªï¼
- ãªãã¸ã§ã¯ã
- ç©ã®ãã¨ãä½ãç©ã§ä½ãç©ã§ãªããã®å®ç¾©ã¯å²å¦çãªã®ã§æ£ç´è¯ãåãããªããRubyã§ã¯å ¨ã¦ããªãã¸ã§ã¯ãã§ï¼ä¾ãã° 1234 ã¨ã "hello, world" ã¨ãããªãã¸ã§ã¯ãã¨å¼ã¶ã
- ã¤ã³ã¹ã¿ã³ã¹
- ãªãã¸ã§ã¯ãã®ãã¨ãæããªãã¸ã§ã¯ããæãã¯ã©ã¹ã«å±ãã¦ããã¨ãï¼å±ãã¦ãããã¨ã強調ãã¦ãã®ãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹ã¨å¼ã¶ãä¾ãã° "hello, world" 㯠String ã®ã¤ã³ã¹ã¿ã³ã¹ã ã
- ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ãã
- ã¡ã½ããï¼é¢æ°ï¼ã®ãã¨ãã¤ã³ã¹ã¿ã³ã¹ããå¼ã¶ã¡ã½ããã強調ãã¦ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããã¨å¼ã¶ã ä¾ãã°Stringã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããreverseã¯ï¼ã¤ã³ã¹ã¿ã³ã¹ã«ãããã§ç¹ã㦠"hello, world".reverse ã¨å¼ã³åºãã
- #
- RubistããString#reverseãã¨è¨ã£ããããã¯ãStringã®ã¤ã³ã¹ã¿ã³ã¹ã¡ã½ããreverseãã¨ããæå³ã ã
- ãããã¯
- RubyãRubyè³ãããã¦ããåãæç¨ãªè¨æ³ãRubyã§ã¯ãããã¯ãåå «çªã®ããã«ä½¿ããï¼å®ã¯Rubyã®ãªãªã¸ãã«ã§ã¯ç¡ãã¦å ãã¿ã¯CLUã