en

Day 2 ~ 13ļ¼š study with the Ruby certification exam

updated at Mar 24, 2019
post cover image

ć‚½ćƒ¼ć‚¹ć‚³ćƒ¼ćƒ‰ć®ę–‡å­—ć‚³ćƒ¼ćƒ‰ć‚’US-ASCII恫čØ­å®šć®ćƒžć‚ø惃ć‚Æć‚³ćƒ”ćƒ³ćƒˆ

# 1. coding: us-ascii
# 2. encoding: us-ascii
# 3. -*- charset: us-ascii -*-
# 4. CODING: US-ASCII

1ļ¼Œ2ļ¼Œ4ćÆ同ē¾©ć§ć€ę­£č§£ć€‚3ćÆPython怂

例外処ē†ļ¼ˆbegin rescue ensure end

Wikipediać«ć‚ˆć‚‹ćØ

ćƒ—ćƒ­ć‚°ćƒ©ćƒ ć®äøŠä½ć®å‡¦ē†ć‹ć‚‰å‘¼ć³å‡ŗć•ć‚Œć¦ć„ć‚‹äø‹ä½ć®å‡¦ē†ć§ē¶™ē¶šäøčƒ½ć€ć¾ćŸćÆē¶™ē¶šć™ć‚Œć°ę”Æ障悒恍恟恙ē•°åøøäŗ‹ę…‹ć«é™„ć£ćŸćØćć€åˆ¶å¾”ć‚’å‘¼ć³å‡ŗć—å…ƒć®äøŠä½ć®å‡¦ē†ć«čæ”ć—å®‰å…ØćŖēŠ¶ę…‹ć«ćŖć‚‹ć‚ˆć†å›žå¾©å‡¦ē†ć‚’恙悋恓ćØ怂

begin
  # ä¾‹å¤–ćŒčµ·ć“ć‚Šć†ć‚‹ć‚³ćƒ¼ćƒ‰1
rescue => # å¤‰ę•°(例外ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ä»£å…„)
  # ä¾‹å¤–ćŒčµ·ććŸå “åˆć®ć‚³ćƒ¼ćƒ‰2
ensure
  # ä¾‹å¤–ć®ęœ‰ē„”ć«é–¢ć‚ć‚‰ćšć€å®Ÿč”Œć™ć‚‹ć‚³ćƒ¼ćƒ‰3 
end

1ć§ä¾‹å¤–ćŒčµ·ććŖć‹ć£ćŸć‚‰ć€č”Øē¤ŗēµęžœćÆ1äø”恤3恮č”Øē¤ŗēµęžœć€čµ·ććŸå “合ćÆ2äø”恤3恮č”Øē¤ŗēµęžœćØćŖ悋怂
例外処ē†ć‚³ćƒ¼ćƒ‰ćØ恗恦态ensure恌ē”Ø恄悉悌ćŖ恄惑ć‚æćƒ¼ćƒ³ć‚‚ć‚ć‚‹ć€‚
ćć®ä»–ć€rescue恮äø­ć§retry悒ē”Ø恄态beginćŒęˆåŠŸć™ć‚‹ć¾ć§ē¹°ć‚Ščæ”ć™å‡¦ē†ćŒć‚‹ć€‚(ē„”é™ćƒ«ćƒ¼ćƒ—ę³Øꄏļ¼‰

å®šę•°ćØå¤‰ę•°

äø‹ć‚³ćƒ¼ćƒ‰ć®å®Ÿč”ŒēµęžœćÆ怎10ćØč”Øē¤ŗć•ć‚ŒćŸå¾Œć€č­¦å‘ŠćŒč”Øē¤ŗ恕悌100ćØč”Øē¤ŗ恕悌悋怂怏

MAX=10
print MAX
MAX=100
print MAX

Rubyć«ćŠć„ć¦ć€ć‚¢ćƒ«ćƒ•ć‚”ćƒ™ćƒƒćƒˆå¤§ę–‡å­—ć‹ć‚‰å§‹ć¾ć‚‹č­˜åˆ„å­(ABC悄Abc)ćÆå®šę•°ć§ć‚ć‚‹ć€‚
他č؀čŖžć§ćÆå®šę•°ć‚’å†å®šē¾©ć—ć‚ˆć†ćØ恙悋ćØć€č­¦å‘ŠćŒå‡ŗ恦态ēµęžœć€å†å®šē¾©ć§ććŖ恄怂
Ruby恫恊恄恦ćÆć€č­¦å‘ŠćŒå‡ŗćŸć®ć”ć€ćć®ć¾ć¾ćƒ‡ćƒ¼ć‚æ恌ć‚Ŗćƒ¼ćƒćƒ¼ćƒ©ć‚¤ćƒˆć•ć‚Œć‚‹

åÆå¤‰é•·å¼•ę•°

def foo(*a)
  p a
end
foo(1,2,3)
#å®Ÿč”Œēµęžœć€€[1,2,3]

*悒1å€‹ä»˜ć‘ć‚‹ćØć€å¼•ę•°ć‚’č¤‡ę•°ęŒ‡å®šć§ćć€å¼•ę•°ćÆ配列ćØć—ć¦å—ć‘å–ć‚‰ć‚Œć‚‹ć€‚
*悒2å€‹ä»˜ć‘ć‚‹ćØ态ć‚Ŗćƒ—ć‚·ćƒ§ćƒ³å¼•ę•°ćØćŖ悊态HashćØć—ć¦å—ć‘å–ć‚‰ć‚Œć‚‹ć€‚

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćØeach

.to_i

ę–‡å­—åˆ—ć‚’10é€²ę•°ć®č”Øē¾ćØ見ćŖć—ć¦ę•“ę•°ć«å¤‰ę›ć—ć¾ć™ć€‚

#å¼•ę•°baseć‚’å–ć‚‹ć“ćØć‚‚ć§ćć€ćć®å¼•ę•°ć§ä½•é€²ę•°ć‹ć‚’ē¤ŗ恙怂
#ćƒ‡ćƒ•ć‚©ćƒ«ćƒˆå€¤ćÆ10ć§ć‚ć‚Šć€å¼•ę•°ęŒ‡å®šć—ćŖć„å “åˆćÆ10é€²ę•°ćØćŖ悋怂
å¤‰ę•°.to_i(2)

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈

def hoge
  x = 0
  (1...5).each do |i|
    x += 1
  end
  puts x
end
hoge
#å®Ÿč”Œēµęžœć€€4

ć¾ćšć€Žę•°A...ꕰBć€ć«é–¢ć—ć¦ć€ć“ć‚ŒćÆA恋悉Bć¾ć§ē¹°ć‚Ščæ”ć™ć®ę„ć€‚ćŸć ć—ć€BćÆå«ć¾ć‚ŒćŖ恄怂
ćć®ćŸć‚ć€ć€Ž1...5怏ćÆ态1恋悉4ć¾ć§ć®4回分ē¹°ć‚Ščæ”恙怂
äø€ę–¹ć§ć€ć€Ž1..5怏ćÆ5ć‚’å«ćæ态1恋悉5ć¾ć§ć®5回分ē¹°ć‚Ščæ”恙怂
因ćæ恫态怎1..5怏怎101..105怏怎2001..2005怏恠悍恆ćØ5å›žåˆ†ć€‚

upto

money = 1000
ć‚Ŗ惖ć‚ø悧ć‚Æ惈.upto(max) do |i|
  å®Ÿč”Œå‡¦ē†
end
 
money = 100
1.upto(3) do |i|
  money = money * 1.1
end
puts money.to_i

uptoćƒ”ć‚½ćƒƒćƒ‰ćÆć€å¤‰ę•°ć«ć€ŒåÆ¾č±”ć‚Ŗ惖ć‚ø悧ć‚ÆćƒˆćŒęŒć¤ę•°å€¤ć€ć‹ć‚‰ć€Œmaxć€ć¾ć§ć‚’é †ć«ä»£å…„ć—ćŖćŒć‚‰ć€å‡¦ē†å®Ÿč”Œć€‚
1回ē¹°ć‚Ščæ”恙ęÆŽć«1ćšć¤ę•°å€¤ćÆå¢—åŠ ć—ć¾ć™ć€‚ļ¼ˆć“恓恧ćÆļ¼‘恋悉ļ¼“ć¾ć§ć®3å›žć€‚ć€Ž1..3怏ćØ同ē¾©ć€‚
ć¾ćŸć€downtoćƒ”ć‚½ćƒƒćƒ‰ć‚‚ć‚ć‚Šć€ć“ć‚ŒćÆ1回ē¹°ć‚Ščæ”恙恔ćØ恫1ćšć¤ę•°å€¤ćŒęø›å°‘恙悋怂
timesćƒ”ć‚½ćƒƒćƒ‰ć‚‚å‚ē…§ć™ć¹ć—

step

ć‚Ŗ惖ć‚ø悧ć‚Æ惈.step(limit, stepno) do |å¤‰ę•°|
  å®Ÿč”Œć™ć‚‹å‡¦ē†1
  å®Ÿč”Œć™ć‚‹å‡¦ē†2
end

ćÆć˜ć‚ć«ć€stemnoć‚’ć€Žę•°å€¤é–“éš”ć€ćØ恙悋ćØ悏恋悊悄恙恄怂
åÆ¾č±”ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’ęŒć¤ę•°å­—ć‹ć‚‰é †ć«ä»£å…„ć—ćŖćŒć‚‰ć€å‡¦ē†ć‚’å®Ÿč”Œć€‚
1回ē¹°ć‚Ščæ”恙恔ćØ恫态stepnoć§ęŒ‡å®šć—ćŸę•°å€¤é–“éš”ć”ćØć«ć€ę•°å€¤ćŒå¢—åŠ ć™ć‚‹ć€‚

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈态uptoćƒ”ć‚½ćƒƒćƒ‰ć¾ćØ悁

money = 1000
rate = 1.1
2001.upto(2004) do |i|
  money = money * rate
  puts "I got #{money.to_i} in #{i}."
end
#å®Ÿč”Œēµęžœ
# I got 1100 in 2001.
# I got 1210 in 2002.
# I got 1331 in 2003.
# I got 1464 in 2004.

恓悌ćÆ态äø‹ćØ悂ę›ø恑悋

money = 1000
rate = 1.1
(2001..2004).each do |i|
  money *= rate
  puts "I got #{money.to_i} in #{i}."
end

4ę—„ē›®

å¤‰ę•°ć€å®šę•°ć„ć‚ć„ć‚

  • ćƒ­ćƒ¼ć‚«ćƒ«å¤‰ę•°
    • ć‚¢ćƒ«ćƒ•ć‚”ćƒ™ćƒƒćƒˆå°ę–‡å­—ć¾ćŸćÆ`_'ć§å§‹ć¾ć‚‹č­˜åˆ„å­
  • ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°
    • $ć§å§‹ć¾ć‚Šć€å®£čØ€ćŒåæ…要ćŖćć€ćƒ—ćƒ­ć‚°ćƒ©ćƒ ć®ć©ć“ć‹ć‚‰ć§ć‚‚å‚ē…§ć§ćć‚‹ć€‚
    • åˆęœŸåŒ–ć•ć‚Œć¦ć„ćŖć„ć‚°ćƒ­ćƒ¼ćƒćƒ«å¤‰ę•°ć‚’å‚ē…§ć—ćŸå “åˆć€å€¤ćÆnil怂
  • ć‚¤ćƒ³ć‚¹ć‚æćƒ³ć‚¹å¤‰ę•°
    • @ć§å§‹ć¾ć‚Šć€ē‰¹å®šć® ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć«å±žć™ć‚‹ć€‚
    • ćć®ć‚Æćƒ©ć‚¹ć¾ 恟ćÆć‚µćƒ–ć‚Æćƒ©ć‚¹ć®ćƒ”ć‚½ćƒƒćƒ‰ć‹ć‚‰å‚ē…§ć§ćć¾ć™ć€‚
    • åˆęœŸåŒ–ć•ć‚Œć¦ć„ćŖć„ć‚¤ćƒ³ć‚¹ć‚æćƒ³ć‚¹å¤‰ę•°ć‚’å‚ē…§ć—ćŸå “åˆć€å€¤ćÆnil怂
  • ć‚Æćƒ©ć‚¹å¤‰ę•°
    • @@ć§å§‹ć¾ć‚Šć€ć‚Æćƒ©ć‚¹å®šē¾© 恮äø­ć§å®šē¾©ć•ć‚Œć‚‹
    • ć‚Æćƒ©ć‚¹ć®ē‰¹ē•°ćƒ”ć‚½ćƒƒćƒ‰ć€ć‚¤ćƒ³ć‚¹ć‚æćƒ³ć‚¹ćƒ”ć‚½ćƒƒćƒ‰ćŖć©ć‹ć‚‰å‚ē…§ļ¼ä»£å…„恌恧恍悋怂
  • å®šę•°
    • ć‚¢ćƒ«ćƒ•ć‚”ćƒ™ćƒƒćƒˆå¤§ę–‡å­—ć§å§‹ć¾ć‚‹ć€‚
    • 他恮č؀čŖžć§ćÆć€Žå®šę•°ć€ćŖć®ć§ć€å†å®šē¾©ć—ć‚ˆć†ćØć—ć¦ć‚‚ć€č­¦å‘ŠćŒå‡ŗć¦ć€å†å®šē¾©ć§ććŖ恄怂
    • Ruby恧ćÆć€å†å®šē¾©ć—ć‚ˆć†ćØ恙悋ćØć€č­¦å‘ŠćŒå‡ŗć‚‹ćŒć€å†å®šē¾©ć§ćć¦ć—ć¾ć†ć€‚

super

č¦Ŗć‚Æćƒ©ć‚¹ć®ćƒ”ć‚½ćƒƒćƒ‰ć‚’å‘¼ć³å‡ŗ恙

class User 
  attr_accessor:say
  def initialize
    @say = "Hello"
  end
end
class Oriver < User
  def initialize
    super
  end
end

oriver = User.new
puts oriver.say
#å®Ÿč”Œēµęžœļ¼šHello
#恓恮Ꙃ态superćƒ”ć‚½ćƒƒćƒ‰ćÆč¦Ŗć‚Æćƒ©ć‚¹ć®User恋悉态@sayć‚’å‘¼ć³å‡ŗ恗恦恄悋

attr_accerssor(attr_readerćØattr_writer)

  • attr_accerssorćÆć‚²ćƒƒć‚æćƒ¼ćØć‚»ćƒƒć‚æćƒ¼ć®ę©Ÿčƒ½ćŒć‚ć‚‹ć‚¢ć‚Æć‚»ć‚¹ćƒ”ć‚½ćƒƒćƒ‰
  • ć‚¤ćƒ³ć‚¹ć‚æćƒ³ć‚¹å¤‰ę•°ć®å€¤ćÆć‚Æćƒ©ć‚¹å†…ć‹ć‚‰ć—ć‹å–å¾—å‡ŗę„ćŖ恄怂
  • ć‚Æćƒ©ć‚¹å¤–ć‹ć‚‰ć‚¤ćƒ³ć‚¹ć‚æćƒ³ć‚¹å¤‰ę•°ć‚’å–å¾—ę›“ę–°ć™ć‚‹ć«ćÆć€ć‚²ćƒƒć‚æćƒ¼ć€ć‚»ćƒƒć‚æćƒ¼ćŒåæ…要
    • Progate恮PHPć®ę–¹ćŖ悉čŖ¬ę˜ŽćŒć”悃悓ćØć‚ć£ćŸć€‚

include (ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«å, ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«å, ...)

ć‚Æćƒ©ć‚¹ć‚„ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć«ć€ä»–ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ć‚¤ćƒ³ć‚Æćƒ«ćƒ¼ćƒ‰ć™ć‚‹ć€‚
å¼•ę•°ć«ćÆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ęŒ‡å®šć—ć€äøŠć®ę§˜ć«č¤‡ę•°ęŒ‡å®šć™ć‚‹ć“ćØ恌恧恍悋

Math module

ęµ®å‹•å°ę•°ē‚¹ę¼”ē®—ć‚’ć‚µćƒćƒ¼ćƒˆć™ć‚‹ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«.

#å††ć®é¢ē©
include Math
def area r
  return r * r * PI
end

#ēƒć®ä½“ē©
def volume r
  return (4/3) * r * r * r * PI
end 

#Ļ€ć®å€¤
puts PI怀#ēµęžœļ¼š3.141592653589793
area(2)怀#ēµęžœļ¼š12.566370614359172
volume (2)怀怀: 25.132741228718345

恓悌ćÆä¾æåˆ©ć€‚


7ę—„ē›®

ę¼”ē®—å­ć®ć‚Ŗćƒ¼ćƒćƒ¼ćƒ©ć‚¤ćƒ‰

Ruby ć®å “åˆć€ę¼”ē®—å­ć®å¤šććŒćŸć ć®ćƒ”ć‚½ćƒƒćƒ‰ćØć—ć¦å®šē¾©ć€‚

再定ē¾©ć§ćć‚‹ę¼”ē®—子

|  ^  &  <=>  ==  ===  =~  >   >=  <   <=   <<  >>
+  -  *  /    %   **   ~   +@  -@  []  []=  ` ! != !~

再定ē¾©äøåÆć®ę¼”ē®—子

ę¼”ē®—å­ć®ēµ„åˆć›ć§ć‚ć‚‹č‡Ŗå·±ä»£å…„ę¼”ē®—子ćÆ再定ē¾©ć§ććŖ恄怂

=  ?:  ..  ...  not  &&  and  ||  or  ::

ę¼”ē®—å­ć®å®šē¾©ę–¹ę³•

# äŗŒé …ę¼”ē®—子
def +(other)                # obj + other
def -(other)                # obj - other

# å˜é …ćƒ—ćƒ©ć‚¹/ćƒžć‚¤ćƒŠć‚¹
def +@                      # +obj
def -@                      # -obj

# 要ē“ ä»£å…„
def foo=(value)             # obj.foo = value

# [] ćØ []=
def [](key)                 # obj[key]
def []=(key, value)         # obj[key] = value
def []=(key, key2, value)   # obj[key, key2] = value

# 惐惃ć‚Æć‚Æć‚©ćƒ¼ćƒˆčØ˜ę³•
def `(arg)                  # `arg` ć¾ćŸćÆ %x(arg)

ćƒ”ć‚½ćƒƒćƒ‰ć®ć‚Ŗćƒ¼ćƒćƒ¼ćƒ©ć‚¤ćƒ‰ćØęÆ”č¼ƒę¼”ē®—子

##ęÆ”č¼ƒę¼”ē®—子ćØ<<怀怀ļ¼šå•é”Œ
class Employee
  attr_reader :id
  attr_accessor :name
  
  def initialize id, name
    @id = id
    @name = name
  end
  
  def to_s
    return "#{@id}:#{@name}"
  end
  #ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ恗恦äø‹ć•ć„怂
end

employees = []
employees << Employee.new("3","Tanaka")
employees << Employee.new("1","Suzuki")
employees << Employee.new("2","Sato")
employees.sort!
employees.each do |employee| puts employee end

# å®Ÿč”Œēµęžœ
1:Suzuki
2:Sato
3:Tanaka

# éøęŠžč‚¢
# 1 (ē­”ćˆļ¼‰
def <=> other
  return self.id <=> other.id
end

ęÆ”č¼ƒę¼”ē®—子 <=>

| 1 | 2 |
|:-:|:-:|
| a < b | -1 |
| a == b | 0 |
| a > b | 1 |
| ęÆ”č¼ƒäøåÆ | nil |

<< ćØćÆ

<<<ę¼”ē®—子ļ¼ˆćƒ”ć‚½ćƒƒćƒ‰ļ¼‰ćÆć€å·¦č¾ŗć®é…åˆ—ļ¼ˆćƒ¬ć‚·ćƒ¼ćƒļ¼‰ć®ęœ«å°¾ć«å³č¾ŗ恮ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć‚’č¦ē“ ćØć—ć¦åŠ ćˆć¾ć™ć€‚

#例
animals = ["dog", "cat", "mouse"]
animals << "pig"
p animals
["dog", "cat", "mouse", "pig"]

&& & and ćØ || | or 恮違恄

#問锌
a = [1,2,3,4]
b = [1,3,5,7]
#ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ恗恦äø‹ć•ć„怂
c = XXXX
c.each {|i| print i, " " }
#å‡ŗ力ēµęžœ
1  3
#éøęŠžč‚¢
1. a+b ,2. a-b  ,3. a & b  ,4. a && b ,  5. a | b , 6. a || b
#č§£ē­”怀3
  • &&ę¼”ē®—子
    • å·¦č¾ŗć®č©•ä¾”ēµęžœćŒfalseć®å “åˆćÆć€å³č¾ŗć‚’č©•ä¾”ć›ćšć«ę¬”ć®å‡¦ē†ć«é€²ć‚€
  • || ę¼”ē®—子
    • å·¦č¾ŗć®č©•ä¾”ēµęžœćŒtrue ć®å “åˆćÆć€å³č¾ŗć‚’č©•ä¾”ć›ćšć«ę¬”ć®å‡¦ē†ć«é€²ć‚€
  • |ę¼”ē®—子ćØ&ę¼”ē®—子
    • å·¦č¾ŗć®č©•ä¾”ēµęžœć«é–¢äæ‚ćŖćć€å³č¾ŗć‚’č©•ä¾”ć™ć‚‹

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈

#ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈ē­‰ļ¼šå•é”Œ
a = [1,2,3,4]
#ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ恗恦äø‹ć•ć„怂
a.each do |i|
  print i, " "
end
#å®Ÿč”Œēµęžœ
1 2 3

# éøęŠžč‚¢
1. a[0..3]  ,2. a[0..-2]  ,3. a[0...-2]  ,4. a[0...-3] ,
5. a[3,0]  , 6. a[0,3]    ,7. a[2,0]       ,8. a[0,2]
# ē­”ćˆćÆ2ćØ6

é…åˆ—ć®ćƒ”ć‚½ćƒƒćƒ‰

適切ćŖ悂恮悒éøć¹

# 問锌éøęŠžč‚¢
1. find_allćØselectćÆåŒć˜å‹•ä½œć‚’ć™ć‚‹ć€‚
2. mapćØcollectćÆåŒć˜å‹•ä½œć‚’ć™ć‚‹ć€‚
3. findćØselectćÆåŒć˜å‹•ä½œć‚’ć™ć‚‹ć€‚
4. firstćÆå…ˆé ­č¦ē“ ć‚’å‰Šé™¤ć—ć€ćć®č¦ē“ ć‚’čæ”恙怂
5. concatćÆ非ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰ć§ć‚ć‚‹ć€‚
6. concat!ćÆē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰ć§ć‚ć‚‹ć€‚

#č§£ē­”
1ćØ2

select ćØ find_all

åŒć˜å‹•ä½œć‚’ć€ę”ä»¶ć«åˆć†é…åˆ—ć‚’ćć®ć¾ć¾å‡ŗ力

#select
[1, 2, 3, 4, 5].select{ |v| v % 2 == 1 }
=> [1, 3, 5]
#find_all
[1, 2, 3, 4, 5].find_all{ |v| v % 2 == 1 }
=> [1, 3, 5]

map ćØ怀collect

åŒć˜å‹•ä½œć‚’ć—ć€ę”ä»¶ć«ć‚ć‚‹é…åˆ—ć‚’true or false恧å‡ŗ力

#map
[1, 2, 3, 4, 5].map{ |v| v % 2 == 1 }
=> [true, false, true, false, true]
#collect
[1, 2, 3, 4, 5].collect{ |v| v % 2 == 1 }
=> [true, false, true, false, true]

find

findćÆå…ˆé ­č¦ē“ ć ć‘å‡ŗ力

#find
[1, 2, 3, 4, 5].find{ |v| v % 2 == 1 }
=> 1

ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰concat

concat!ćÆ定ē¾©ć•ć‚Œć¦ć„ćŖ恄

val = [ "a", "b" ]
c=> ["a", "b"]
val.concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
=> ["a", "b", "c", "d"]l
puts val
a
b
c
d

ęœ€å¾Œć€valć®å€¤ćŒå¤‰åŒ–ć—ć¦ć„ć‚‹ę‰€ć€ć€č¦ę³Øꄏ

zip[array]

ć‚³ćƒ¼ćƒ‰ēµęžœć‚’éøć¹

a1=%w(a b)
a2=%w(x y)
a3=a1.zip(a2)
p a3.first

# éøęŠžč‚¢
1. "a"  ,2. ["a"]    ,3. ["a", "x"]  ,4. ["a", "b"]  ,5. ä¾‹å¤–ćŒē™ŗē”Ÿć™ć‚‹ć€‚
#ć€€č§£ē­”

%w( )

é…åˆ—ć‚’ä½œć‚‹ć€‚é…åˆ—ć®č¦ē“ ćÆć‚¹ćƒšćƒ¼ć‚¹åŒŗåˆ‡ć‚Šć§ęŒ‡å®šć™ć‚‹ć€‚
å¼ć®å±•é–‹ćÆ恕悌ćŖ恄怂

zip array

instance method Array#zip

zipćƒ”ć‚½ćƒƒćƒ‰ćÆć€é…åˆ—ć®č¦ē“ ć‚’å¼•ę•°ć®é…åˆ—other_array恮要ē“ ćØēµ„ćæåˆć‚ć›ć€é…åˆ—ć®é…åˆ—ć‚’ä½œęˆć—ć¦čæ”ć—ć¾ć™ć€‚č”ŒćØåˆ—ć‚’å…„ć‚Œę›æćˆć‚‹ćŒć€č¶³ć‚ŠćŖ恄要ē“ ćÆnilć§åŸ‹ć‚ć‚‰ć‚Œć€ä½™åˆ†ćŖ要ē“ ćÆęØć¦ć‚‰ć‚Œć¾ć™ć€‚

arr1 = [1, 2, 3]
arr2 = [4, 5]
arr3 = [6, 7, 8, 9]
p arr1.zip(arr2, arr3)
[[1, 4, 6], [2, 5, 7], [3, nil, 8]]

slice()

å®Ÿč”Œēµęžœć‚’恋恑

a = [1,2,3,4,5]
p a.slice(1,3)

#ē­”ćˆ

slice()

č¦å‚ē…§

slicećƒ”ć‚½ćƒƒćƒ‰ćÆ态[]ćƒ”ć‚½ćƒƒćƒ‰ć®åˆ„åć§ć™ć€‚é…åˆ—ć‹ć‚‰å¼•ę•°ć§ęŒ‡å®šć—ćŸä½ē½®ć®č¦ē“ ć‚’å–ć‚Šå‡ŗ恗恦čæ”恙怂

equal

å®Ÿč”Œēµęžœć‚’ę›ø恑

a = "abc"
b = "abc"
print a.eql? b
print a.equal? b
print a == b

# =>
# true  false  true

eql?

ćƒćƒƒć‚·ćƒ„ć®å†…éƒØć§ć€ŒåŒć˜ć‚­ćƒ¼ć‹ć©ć†ć‹ć€ć‚’čŖæć¹ć‚‹ćŸć‚ć«ä½æć‚ć‚Œć‚‹ćƒ”ć‚½ćƒƒćƒ‰ć§ć€ä»Šå›žćÆćƒ¬ć‚·ćƒ¼ćƒabcćŒå¼•ę•°abcćØåŒć˜ć‹ć©ć†ć‹čŖæć¹ć¦ć„ć‚‹

eqal?(object)

ćƒ¬ć‚·ćƒ¼ćƒobjćØå¼•ę•°other_objćŒåŒć˜ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćŖ悉trueć€åˆ„ć®ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćŖ悉false悒čæ”恙怂
objentēµ”ćæćŖć®ć§ć€å¤‰ę•°åćŒé•ć†ćØ态false悒čæ”ć™ć®ć ć‚ć†ć€‚ć€‚ć€‚ļ¼Ÿ

åž‹ć®å–ć‚Šę‰±ć„ļ¼šć‚Øćƒ©ćƒ¼ć«ćŖ悉ćŖ恄悂恮悒éøć¹

select

  1. puts 5 + "Hello"
  2. puts "Hello" + 5
  3. puts "Hello" * 5
  4. puts 5 * "Hello"

integerćØstring

å‹˜é•ć„ć—ć¦ć„ćŸ
ä¾‹ćˆć°ć€puts hoge + "恻恒"ć§ć‚ć£ć¦ć‚‚ć€hogećŒę•“ę•°åž‹ć®å¤‰ę•°ćŖ悉态hoge恻恒ćØčæ”恙

concatćØchopćØchompļ¼šå®Ÿč”Œēµęžœć‚’ę›ø恑

s1 = "Hoge"
s2 = "Fuga"
s1.concat(s2)
s1.chop
s1.chomp
s1 + s2
puts s1

# => ?

concat

ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰ć€‚<< 悄怀pushć«ä¼¼ć¦ć„ć‚‹

chop

ę–‡å­—åˆ—ć®ęœ«å°¾ć‹ć‚‰1ę–‡å­—ć‚’å–ć‚Šé™¤ć„ćŸę–°ć—ć„ę–‡å­—åˆ—ć‚’čæ”恙怂
\r\n 恧ēµ‚ć‚ć£ć¦ć„ć‚‹å “åˆć ć‘ćÆ态 \r\n 恮2ę–‡å­—ć‚’å–ć‚Šé™¤ćć¾ć™ć€‚

chomp

ę–‡å­—åˆ—ć®ęœ«å°¾ć®ę”¹č”Œę–‡å­—ć‚’å–ć‚Šé™¤ć„ćŸę–°ć—ć„ę–‡å­—åˆ—ć‚’čæ”恙怂
ę”¹č”Œę–‡å­—ćÆ"\n"态"\r\n"态"\r"ć®ć©ć‚Œć§ć‚‚å–ć‚Šé™¤ćć¾ć™

7ę—„ē›®ć®ęµć‚Œ

# irb
s1 = "Hoge"
s2 = "Fuga"

s1.concat(s2)
# .c=> "HogeFuga"
# chop恧s1ęœ«å°¾ć®ećŒå–ć‚Šé™¤ć‹ć‚Œć‚‹
s1.chop
# => "HogeFug"
# s1ć«ę”¹č”Œę–‡å­—ćÆå«ć¾ć‚Œć¦ć„ćŖ恄
s1.chomp  
# => "HogeFuga"
# ę–‡å­—åž‹åŒå£«ć®å’ŒćÆåÆčƒ½
s1 + s2
# => "HogeFugaFuga"
# concat恮ē “壊ēš„ꀧč³Ŗć«ć‚ˆć‚Šć€s1.concat(s2)恮Ꙃē‚¹ć§ć€s1恌HogeFuga恫怂
puts s1
# HogeFuga

[]ćƒ”ć‚½ćƒƒćƒ‰

s = "123456789"
p s[1,4]

# =>
# "2345"

ć¾ćšć€ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®é”žć ćØę€ć£ć¦ć„ćŸć€‚

č¦å‚ē…§: [] (Array)

slice()ć®åˆ„å

Hashć‚Æćƒ©ć‚¹

select

  1. {}恧ē©ŗ恮Hashć‚Ŗ惖ć‚ø悧ć‚Æ惈悒ē”Ÿęˆć§ćć‚‹ć€‚
  2. []恧ē©ŗ恮Hashć‚Ŗ惖ć‚ø悧ć‚Æ惈悒ē”Ÿęˆć§ćć‚‹ć€‚
  3. invertć§ć‚­ćƒ¼ćØå€¤ć‚’å…„ć‚Œę›æ恈悋恓ćØ恌恧恍悋怂
  4. key恫åƾåæœć™ć‚‹å€¤ć‚’å–ć‚Šå‡ŗć™ćƒ”ć‚½ćƒƒćƒ‰ćØ恗恦态has_key?(key)态key?(key)态get_ key(key)ćŖć©ćŒē”Øę„ć•ć‚Œć¦ć„ć‚‹ć€‚
  5. ć™ć¹ć¦ć®č¦ē“ ć‚’å‰Šé™¤ć™ć‚‹ć«ćÆ态delete_all悒ä½æē”Ø恙悋怂

=> ans
ļ¼“ćØļ¼’(ē­”ćˆć‚’åæ˜ć‚ŒćŸć€‚å¾Œć§ć¾ćŸčŖæć¹ć‚‹

Hashć‚Æćƒ©ć‚¹

ćƒćƒƒć‚·ćƒ„ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®å…ƒć«ćŖć£ć¦ć„ć‚‹ć‚Æćƒ©ć‚¹

[]

[]ćƒ”ć‚½ćƒƒćƒ‰ćÆć€é…åˆ—ć‹ć‚‰å¼•ę•°ć§ęŒ‡å®šć—ćŸä½ē½®ć®č¦ē“ ć‚’å–ć‚Šå‡ŗ恗恦čæ”ć—ć¾ć™ć€‚å¼•ę•°ć«ćÆ态ꕓꕰļ¼ˆä½ē½®ļ¼‰ć€ę•“ꕰ2恤ļ¼ˆä½ē½®ćØꕰļ¼‰ć€ēÆ„å›²ć‚’ęŒ‡å®šć§ćć¾ć™ć€‚

{}

Hashä½œęˆ

invert

ć‚­ćƒ¼ć‚’å€¤ć«ć€å€¤ć‚’ć‚­ćƒ¼ć«ć—ćŸćƒćƒƒć‚·ćƒ„ć‚’ä½œęˆ

key

has_key?ćƒ”ć‚½ćƒƒćƒ‰ćÆć€ćƒćƒƒć‚·ćƒ„ćŒå¼•ę•°keyćØåŒć˜ć‚­ćƒ¼ć‚’ęŒć£ć¦ć„ć‚Œć°true态ćŖ恑悌恰false悒čæ”ć—ć¾ć™ć€‚
key?ćƒ”ć‚½ćƒƒćƒ‰ć€include?, member?ćƒ”ć‚½ćƒƒćƒ‰ćÆ态has_key?ć®åˆ„å

delete_all

ꤜē“¢ć§delete hashćÆ惒惃惈恙悋恌态delete_all hash ćÆ惒惃惈恗ćŖ恄

Hashć‚Æćƒ©ć‚¹

select uncorrect

  1. deletećÆć‚­ćƒ¼ć‚’ęŒ‡å®šć—ć¦ć‚­ćƒ¼ćØå€¤ć®ēµ„ć‚’å‰Šé™¤ć™ć‚‹ć€‚
  2. removećÆć‚­ćƒ¼ćØå€¤ć®ēµ„ć‚’å‰Šé™¤ć™ć‚‹ć€‚
  3. fetchćÆäøŽćˆć‚‰ć‚ŒćŸć‚­ćƒ¼ć«é–¢é€£ä»˜ć‘ć‚‰ć‚ŒćŸå€¤ć‚’čæ”恙怂
  4. clearćÆćƒćƒƒć‚·ćƒ„ć®č¦ē“ ć‚’å…Øć¦å‰Šé™¤ć™ć‚‹ć€‚

ans => ļ¼‘, 4

delete

ćƒćƒƒć‚·ćƒ„ć‹ć‚‰å¼•ę•°keyćØåŒć˜ć‚­ćƒ¼ć‚’ęŽ¢ć—ć¦ć€ć‚­ćƒ¼ćØå€¤ć‚’å‰Šé™¤ć€‚ćƒ¬ć‚·ćƒ¼ćƒč‡Ŗčŗ«ć‚’å¤‰ę›“ć™ć‚‹ćƒ”ć‚½ćƒƒćƒ‰

fetch

ćƒćƒƒć‚·ćƒ„ć‹ć‚‰å¼•ę•°ć§ęŒ‡å®šć—ćŸć‚­ćƒ¼ć®å€¤ć‚’å–ć‚Šå‡ŗ恗恦čæ”ć—ć¾ć™ć€‚[]ćƒ”ć‚½ćƒƒćƒ‰ćØę©Ÿčƒ½ćÆåŒć˜ć€‚ć ćŒć‚­ćƒ¼ćŒå­˜åœØ恗ćŖ恄ćØćć®å‹•ä½œćŒē•°ćŖ悋

clear

ć‚­ćƒ¼ćØå€¤ć‚’ć™ć¹ć¦å‰Šé™¤ć—ć€ćƒćƒƒć‚·ćƒ„ć‚’ē©ŗć«ć—ć¾ć™ć€‚ćƒ¬ć‚·ćƒ¼ćƒč‡Ŗčŗ«ć‚’å¤‰ę›“ć™ć‚‹ćƒ”ć‚½ćƒƒćƒ‰

Hashć‚Æćƒ©ć‚¹

å®Ÿč”Œēµęžœć‚’恋恑

#{} ć®å•é”Œ
v = {}
v.class
#å‡ŗ力ē­”ćˆć€€Hash

Hashć‚Æćƒ©ć‚¹

å®Ÿč”Œēµęžœć‚’恋恑

#invert array : 問锌
v = {:v1 => 1, :v2 => v}
v.invert
#å‡ŗ力ē­”ćˆć€€{1=>:v1, nil=>:v2}

invert

ę—¢ć«äøŠć§čؘčæ°ęøˆćæ

%ē­‰ć€…

回ē­”ć‚’éøꊞ恗恋悉éøć¹

member = [10, "Tanaka"]
print "ID:%2d Name:%s" #ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ恗恦äø‹ć•ć„怂  
member
# å®Ÿč”Œēµęžœ
ID:10 Name:Tanaka
# éøęŠžč‚¢
1. ,   ,2. +
3. %    ,4. .format

#ē­”ćˆć€€%

%dćØ%s,%

%d

ę•°å­—ć‚’ę–‡å­—åˆ—ć«ć—ć¦ć„ć‚‹

sprintf("%d", 4);
=> "4"

# %2d 2ę”ć®ę–‡å­—åˆ—ć‚’čæ”恙
sprintf("%2d", 4);
=> " 4"

# %02d 2ę”ć®ę–‡å­—åˆ—ć‚’čæ”ć™ć€‚ę”ę•°ćŒč¶³ć‚ŠćŖć„å “åˆćÆć€å‰ć«0悒čæ½åŠ ć€‚
sprintf("%02d", 4);
=> "04"

%s

ć‚·ćƒ³ćƒœćƒ«:ć‚’ä»˜ć‘ć‚‹ć€‚å¼ć®å±•é–‹ćÆ恕悌ćŖ恄怂

Hashć‚Æćƒ©ć‚¹

適切ćŖéøęŠžč‚¢ć‚’

#é…åˆ—ć€ć‚·ćƒ³ćƒœćƒ«ć€€å•é”Œ
h = #ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ恗恦äø‹ć•ć„怂
p h
#éøęŠžč‚¢
1. {a:1,b:2,c:3}怀怀怀态2. {a=1,b=2,c=3}
3. Hash[:a, 1, :b, 2, :c, 3]怀怀怀怀态4. {:a1,:b2,:c3}
#å®Ÿč”Œēµęžœ
{ :a => 1, :b => 2, :c => 3 }

#ē­”ćˆć€€ļ¼”

8ę—„ē›®

##怀ioć‚Æćƒ©ć‚¹
å®Ÿč”Œēµęžœć‚’

# ćƒ•ć‚”ć‚¤ćƒ«datać®å†…å®¹ćÆabcdefg

File.open("data") do |io|
  while not io.eof?
    print io.read(1)
    io.seek(0,IO::SEEK_SET)
  end
end

#ē­”ćˆ: a恌č”Øē¤ŗ恕悌ē¶šć‘ć‚‹

CPU恌6å‰²ć€ćƒ”ćƒ¢ćƒŖļ¼–GBé£Ÿć‚ć‚Œć¦ćŸć€‚

EOFćØćÆ

End of File恮ē•„
##ioć‚Æćƒ©ć‚¹ćƒ”ć‚½ćƒƒćƒ‰ć€SEEK_SET

instance method IO#seek

ćƒ•ć‚”ć‚¤ćƒ«ćƒć‚¤ćƒ³ć‚æ悒 whence ć®ä½ē½®ć‹ć‚‰ offset 恠恑ē§»å‹•ć•ć›ć¾ć™ć€‚ offset 位ē½®ćø恮ē§»å‹•ćŒęˆåŠŸć™ć‚Œć° 0 悒čæ”ć—ć¾ć™

ćƒ•ć‚”ć‚¤ćƒ«äøŠć®ę–‡å­—ć«ćƒć‚¤ćƒ³ć‚æćŖć‚‹ä½ę‰€ć®ć‚ˆć†ć§ä½ę‰€ć˜ć‚ƒćŖć„ć‚‚ć®ćŒć‚ć‚‹ć€‚ć‚‰ć—ć„
今回ćÆ态io.seek(0,IO::SEEK_SET)恧ć‚Ŗćƒ•ć‚»ćƒƒćƒˆć‚’0ć«ęŒ‡å®šć—ć¦ć„ć‚‹ćŸć‚ć€
ę–‡å­—ć®čŖ­ćæč¾¼ćæćŒęœ€åˆć®ać‹ć‚‰ć™ć™ć¾ćŖ恄ļ¼ˆćšć£ćØa恌čŖ­ćæč¾¼ć¾ć‚Œć‚‹ļ¼‰ć‚‰ć—恄

strgtime

å®Ÿč”Œēµęžœć«ć‚恆态éøęŠžč‚¢ć‚’éøć¹

# 問锌
t = Time.gm(1970,1,1)
puts t.strftime("#ć“ć“ć«ä»£å…„ć™ć‚‹å¼ć‚’éøꊞ")
# => 1970/01/01

#éøęŠžč‚¢
1, %y/%m/%d怀怀怀,2, %Y/%M/%d
3, %y/%M/%d      ,4, %Y/%m/%d

#ē­”ćˆļ¼š4

strgtimećƒ”ć‚½ćƒƒćƒ‰

instance method Time#strftime

strgtimeć§ä»£č”Øēš„ćŖ悂恮

| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
| 4ꔁ恮č„æꚦ | 2ꔁ恮č„æꚦ | ꜈ | ę—„ | ę™‚åˆ» | 分 | ē§’ |
| %Y | %y| %m | %d | %H | %M | %S |

Ruby恮äŗˆē“„čŖž

äŗˆē“„čŖžć‚’éøć¹

# 問锌
while怀怀怀rand
goto怀怀怀class怀怀怀const

#ē­”ćˆć€€whilećØclass

äŗˆē“„čŖž: ę—¢ć«rubyå“ć§å®šē¾©ć•ć‚Œć¦ć„ć‚‹čØ€č‘‰ć€‚č‡Ŗåˆ†ć§ä½æ恊恆ćØ恗恦悂ä½æ恈ćŖ恄怂

to_s

å®Ÿč”Œēµęžœć‚’恋恑

# 問锌
x = 0
[1,2,3].each do |x|
 print x.to_s + " "
end
puts x

#č§£ē­”怀1  2  3  0
# 1ļ¼Œ2ļ¼Œ3ćÆč”Øē¤ŗäøŠćÆę•°å­—åž‹ć ć‹ć€å®ŸćÆę–‡å­—åž‹

to_s怀ļ¼šę•“ę•°åž‹ćƒ‡ćƒ¼ć‚æć‚’ę–‡å­—åž‹ć«å¤‰ę›ć™ć‚‹

ꤜčØ¼ć‚³ćƒ¼ćƒ‰

x = 0
[1,2,3].each do |x|
  print x.to_s + " "
  print x.to_s.class
end
puts x

# =>
# x = 0

[1,2,3].each do |x|
  print x.to_s + " "
  print x.to_s.class
  o_s.class
end
puts x

# 0
# => nil

Rubyć®å½ć®å€¤

#問锌
nil怀怀0怀怀怀""
false怀怀0.0怀怀怀NULL

#č§£ē­”怀怀nil ćØ怀怀false

例外処ē†: 適切ćŖéøęŠžč‚¢ć‚’

# 問锌
y = false
y ##AAA## (raise "failed")
puts("succeeded!")
#éøęŠžč‚¢
1, |怀怀,2, ||   ,3, &    ,4, &&

#ē­”ćˆć€€ć€€ć€€&&

&&ę¼”ē®—子

&&ę¼”ē®—子ćÆå·¦č¾ŗć®č©•ä¾”ēµęžœćŒfalseć®å “åˆćÆć€å³č¾ŗć‚’č©•ä¾”ć›ćšć«ę¬”ć®å‡¦ē†ć«é€²ć‚€
|| ę¼”ē®—子ćÆć€å·¦č¾ŗć®č©•ä¾”ēµęžœćŒtrue ć®å “åˆćÆć€å³č¾ŗć‚’č©•ä¾”ć›ćšć«ę¬”ć®å‡¦ē†ć«é€²ć‚€
|ę¼”ē®—子ćØ&ę¼”ē®—子ćÆć€å·¦č¾ŗć®č©•ä¾”ēµęžœć«é–¢äæ‚ćŖćć€å³č¾ŗć‚’č©•ä¾”ć™ć‚‹

raise 例外

raisećÆä¾‹å¤–ć‚’ęŠ•ć’ć‚‹ć®ć§ć€ćć‚Œä»„é™ć®å‡¦ē†ć‚’é£›ć°ć™
ćć‚Œć‚’ę•ę‰ć—ćŸć„å “åˆćÆ态begin rescue end悒ä½æ恆

ä»Šå›žć®å “åˆć€raisećŒå®Ÿč”Œć•ć‚Œć‚‹ćØputs("succeeded!")ćŒå®Ÿč”Œć•ć‚ŒćŖć„ć®ć‚’ć€č€ƒę…®

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈

1恋悉10ć¾ć§č”Øē¤ŗsć‚‹ć‚³ćƒ¼ćƒ‰ć‚’éøć¹

  1. (1.10).each do |i| puts i end
  2. (1..10).each do |i| puts i end
  3. (1...10).each do |i| puts i end
  4. (1....10).each do |i| puts i end

ans =>怀ļ¼’

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈

定ē¾©ć•ć‚Œć¦ć„悋恮ćÆ态ē‚¹ļ¼’恤ćØē‚¹3恤怂
ļ¼’恤ćÆļ¼’恋悉10仄äø‹ć€3恤ćÆļ¼’恋悉10ęœŖęŗ€

gsub!

å®Ÿč”Œć™ć‚‹ćØ恩恆ćŖ悋

# 問锌
HOGE = "hoge"
HOGE.gsub!("hoge", "piyo")
print HOGE

# ē­”ćˆć€€ć€€č­¦å‘ŠćÆē™ŗē”Ÿć›ćšć€ć€Œpiyo怍恌č”Øē¤ŗ恕悌悋怂

gsub! :ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰

ć‚¢ćƒ«ćƒ•ć‚”ćƒ™ćƒƒćƒˆå¤§ę–‡å­—ć§å§‹ć¾ć‚‹č­˜åˆ„å­ćÆå®šę•°ćÆå®šę•°ćŖć®ć§ć€ę–°ćŸć«ä»£å…„ć‚’č”Œć†éš›ć«ćÆč­¦å‘ŠćŒē™ŗē”Ÿć—ć¾ć™ć€‚
恗恋恗态gsub!ćÆē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰ć‚’å‘¼ć³å‡ŗ恗恦恄悋恠恑ćŖć®ć§ć€č­¦å‘ŠćÆē™ŗē”Ÿć›ćšć€ę–‡å­—列ćÆ怌piyoć€ć«ę›øćę›ć‚ć‚Šć¾ć™ć€‚

例外

ę­£ć—ć„čؘčæ°ć‚’éøć¹

  1. beginć«ć‚ˆć£ć¦ä¾‹å¤–ć‚’ę•ę‰ć™ć‚‹éš›ćÆć€ä¾‹å¤–ć®åÆčƒ½ę€§ćŒć‚ć‚‹å‡¦ē†ć‚’begin恋悉rescue恮間恫čؘčæ°ć™ć‚‹ć€‚
  2. ć²ćØć¤ć®å‡¦ē†ć«åÆ¾ć—ć¦č¤‡ę•°ć®rescuećÆć€ęŒ‡å®šć§ććŖ恄怂
  3. 例外ć‚Ŗ惖ć‚ø悧ć‚Æćƒˆć®ćƒ”ć‚½ćƒƒćƒ‰stacktraceć«ć‚ˆć‚Šć€ä¾‹å¤–ć‚¹ć‚æ惃ć‚Æć‚’å–å¾—ć§ćć‚‹ć€‚
  4. ensurećÆ态åæ…恚čؘčæ°ć™ć‚‹åæ…č¦ćŒć‚ć‚‹

ans => ļ¼‘

stacktracećØćÆ

stack trace - ć€Œåˆ†ć‹ć‚Šćć†ć€ć§ć€Œåˆ†ć‹ć‚‰ćŖć„ć€ć§ć‚‚ć€Œåˆ†ć‹ć£ćŸć€ę°—ć«ćŖ悌悋ITē”ØčŖžč¾žå…ø

ć‚Øćƒ©ćƒ¼ćŒē™ŗē”Ÿć—ćŸćØćć«č”Øē¤ŗć•ć‚Œć‚‹å†…å®¹ć§ć€ćć®ć‚Øćƒ©ćƒ¼ćŒē™ŗē”Ÿć™ć‚‹ć¾ć§ć®éŽē؋ļ¼ˆć©ć‚“ćŖ処ē†ćŒć©ć®é †ē•Ŗć§å‘¼ć³å‡ŗć•ć‚ŒćŸć‹ć®ęµć‚Œļ¼‰ć‚’ć€ć–ć£ćć‚ŠćØč”Øē¤ŗć—ćŸć‚‚ć®

ć‚Øćƒ©ćƒ¼ć‚³ćƒ¼ćƒ‰

仄äø‹ć®ć‚³ćƒ¼ćƒ‰ć® ##AAA## 恫 ZeroDivisionError 恌ē™ŗē”Ÿć—ćŸę™‚ć®å‡¦ē†ć‚’čؘčæ°ć—ćŸć„ć€‚é©åˆ‡ćŖć‚³ćƒ¼ćƒ‰ć‚’éøꊞ

# 問锌
begin
  puts 10 / 0 #ZeroDivisionError
  ##AAA##
  print "ZeroDivisionException:", ex.message
end
  1. rescue ZeroDivisionError = ex
  2. rescue ZeroDivisionError ex
  3. rescue ZeroDivisionError -> ex
  4. rescue ZeroDivisionError => ex

ans => 3

試恗恦ćæ恦

begin
  puts "hello"
  puts 10 / 0
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  rescue ZeroDivisionError => ex
    puts "good bye"
    print "ZeroDivisionException:", ex.message
end
begin
  puts "hello"
  puts 10 / 0
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  puts "good afternoon"
  rescue Exception => ex
    puts "good bye1111111"
    print "message:", ex.message
  rescue ZeroDivisionError => ex
    puts "good bye2222222"
    print "ZeroDivisionException:", ex.message
end

参ē…§: Ruby ä¾‹å¤–ć«ć¤ć„ć¦

?A

ć‚³ćƒ¼ćƒ‰å®Ÿč”Œć©ć†ćŖ悋

# 問锌
p ?A

#ē­”ćˆć€€"A"

=>

#ć‚³ćƒ¼ćƒ‰ļ¼‘怀
p "A".ord
# => 65

#ć‚³ćƒ¼ćƒ‰ļ¼’
p 65.chr 
#=> "A"

65ćØćÆ

unicodeäøŠć®A恮ē•Ŗ号ēš„ćŖ


13ę—„ē›®

ć‚Æćƒ©ć‚¹ć®ē¶™ę‰æ

# ć‚Øćƒ©ćƒ¼ć«ćŖ悉ćŖ恄悂恮悒éøć¹
class Hoge < Object ; end
class Hoge << Object ; end
class Hoge < Kernel ; end
class Hoge << Kernel ; end

# A  ļ¼‘恮ćæ

ć‚Æćƒ©ć‚¹ć®ē¶™ę‰æćÆ<
ćŖ恊态KernelćÆćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ćŖ恮恧äøåÆ

module Kernel

Kernel.call
=>Module

å…Ø恦恮ć‚Æćƒ©ć‚¹ć‹ć‚‰å‚ē…§ć§ćć‚‹ćƒ”ć‚½ćƒƒćƒ‰ć‚’å®šē¾©ć—ć¦ć„ć‚‹ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć€‚ Objectć‚Æćƒ©ć‚¹ćÆć“ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć‚’ć‚¤ćƒ³ć‚Æćƒ«ćƒ¼ćƒ‰ć—ć¦ć„ć¾ć™ć€‚Objectć‚Æćƒ©ć‚¹ć®ćƒ”ć‚½ćƒƒćƒ‰ćÆå®Ÿéš›ć«ćÆć“ć®ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«ć§å®šē¾©ć•ć‚Œć¦ć„ć¾ć™ć€‚

to_i(n)

to_ićƒ”ć‚½ćƒƒćƒ‰ćÆć€ę–‡å­—åˆ—ć‚’né€²ę•°ć®č”Øē¾ćØ見ćŖć—ć¦ę•“ę•°ć«å¤‰ę›ć€‚ę–‡å­—åˆ—ć®å…ˆé ­ć‹ć‚‰10é€²ę•°ćØ見ćŖ恛悋éƒØåˆ†ć‚’åˆ‡ć‚Šå–ć£ć¦å¤‰ę›ć—ć€č¦‹ćŖ恛悋éƒØåˆ†ćŒćŖ恑悌恰0悒čæ”恙怂

p "12abc".to_i =>123
p "abc12".to_i =>0
p "ab12c".to_i =>0
p "1abc2".to_i =>`1

ę­£č¦č”Øē¾

s = "a;b:c;d:e;f"
p s.split (/:|;/)
=>["a", "b", "c", "d", "e", "f"]

配列ćØēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈

a=[1,2,3,4]

a[1,3]    => [2,3,4]
a[1..3]   => [2,3,4]
a[1...3]  => [2,3]
a[1....3] => error
a[2..-1]  => [3,4]

ēƄ囲ć‚Ŗ惖ć‚ø悧ć‚Æ惈ćÆę–‡å­—åˆ—ć«ć‚‚ä½æ恈悋

a="abcdefghijk"
a[1,3] = "x"
p a # =>  "axefghijk"
a[1..3] = "x"
p a # =>  "axefghijk"
a[1...3] = "x"
p a # =>  "axdefghijl"

&ćØ&&态|ćØ||

a = [1,2,3,4]
b = [1,3,5,7]

p a & b =>[1, 3]
p a && b =>[1, 3, 5, 7]
p a | b =>[1, 2, 3, 4, 5, 7]
p a || b =>[1, 2, 3, 4]

| 1 | 作ē”Ø |
|:-:|:-:|
| & | ę—„ęœ¬čŖžć®ā€äø”恤ā€ćØåŒć˜ć€‚ |
| && | å·¦č¾ŗ恌ēœŸćŖć‚‰ć€å³č¾ŗć‚’č©•ä¾”ć—ć¦ć€å³č¾ŗ悒čæ”恙 |
| 惑悤惗1ęœ¬ | ę—„ęœ¬čŖžć®ā€åˆćÆā€ćØåŒć˜ |
| 惑悤惗2ęœ¬ | å·¦č¾ŗ恌ēœŸćŖć‚‰ć€å³č¾ŗć‚’č©•ä¾”ć›ćšć«ć€å·¦č¾ŗ悒čæ”恙 |

ā€»ćƒ‘ć‚¤ćƒ—ļ¼ć€Ž|怏怂ć‚Øć‚¹ć‚±ćƒ¼ćƒ—ć§ććŖć‹ć£ćŸ

åÆå¤‰é•·å¼•ę•°

def sum (##AAA##)
  total = 0
  a.each {|i| total += i }
  return total
end
puts sum(1, 2, 3, 4, 5)
=>15

delete_if = reject!怀ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰

惖惭惃ć‚Æć®ęˆ»ć‚Šå€¤ćŒēœŸć«ćŖć£ćŸč¦ē“ ć‚’å‰Šé™¤ć€‚ćŖ恊态rejectćÆ非ē “壊ēš„

a= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.delete_if{|v| v % 2 == 0 }
=>[1, 3, 5, 7, 9]
p a => [1, 3, 5, 7, 9]

enum.each_with_index {|item, idx| block }

ē¹°ć‚Ščæ”恗恔ćØć«ćƒ–ćƒ­ćƒƒć‚Æå¼•ę•°item恫ćÆå„č¦ē“ ćŒå…„悊态idx恫ćÆ0ļ¼Œ1ļ¼Œ2ćØē•Ŗå·ćŒå…„ć‚‹

a = ["apple", "orange", "grape", "pine"]
a.each_with_index{ |item, i| print i, ":", item, "\n" }
=> 
0:apple
1:orange
2:grape
3:pine

array.each_index {|index| block }

ć€Žć»ć¼ć€each_with_indexćØåŒć˜ć€‚ē¹°ć‚Ščæ”恗恔ćØć«ćƒ–ćƒ­ćƒƒć‚Æå¼•ę•°ć«ćÆå„č¦ē“ ć®ć‚¤ćƒ³ćƒ‡ćƒƒć‚Æć‚¹ļ¼ˆä½ē½®ļ¼‰ć®ę•“ę•°ćŒå…„ć‚Šć¾ć™ć€‚ęˆ»ć‚Šå€¤ćÆćƒ¬ć‚·ćƒ¼ćƒč‡Ŗčŗ«

hash.each_pair {|key, val| block }

eachćØåŒć˜

hash.each_key {|key| block }

ē¹°ć‚Ščæ”恗恔ćØć«ćƒ–ćƒ­ćƒƒć‚Æå¼•ę•°key恫ćÆć‚­ćƒ¼ćŒå…„ć‚‹ć€‚

hash.each_value {|val| block }

ē¹°ć‚Ščæ”恗恔ćØć«ćƒ–ćƒ­ćƒƒć‚Æå¼•ę•°val恫ćÆć‚­ćƒ¼ć®å€¤ćŒå…„ć‚‹ć€‚each_ä½•ćŸć‚‰ćŒä»–ć«ć‚‚ēµę§‹ć‚悋恮恧态見恦恊恓恆

shift ćØunshift ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰

shiftćƒ”ć‚½ćƒƒćƒ‰ćÆć€é…åˆ—ć®ęœ€åˆć®č¦ē“ ć‚’å‰Šé™¤ć—ć€ćć®č¦ē“ ć‚’čæ”ć—ć¾ć™ć€‚ćƒ¬ć‚·ćƒ¼ćƒč‡Ŗčŗ«ć‚’å¤‰ę›“ć™ć‚‹ćƒ”ć‚½ćƒƒćƒ‰ć§ć™ć€‚é…åˆ—ćŒē©ŗ恮ćØ恍ćÆnil悒čæ”ć—ć¾ć™ć€‚

s = ["one","two","three"]

s.shift         => "one" ā€»å…ˆé ­ć®oneć‚’å‰Šé™¤
s.shift         => "two"怀ā€»å…ˆé ­ć®twoć‚’å‰Šé™¤
s.unshift       => ["three"]怀ā€»å¼•ę•°ćŒē„”ć„ć®ć§ć€ä½•ć‚‚ć—ć¦ćŖ恄
s.push "four"   => ["three", "four"]
p s             =>["three", "four"]

compactćØuniq

a = [:a,:a,:b,:c]
a[5] = :e
a.concat([:a,:b,:c])
a.compact
a.uniq
p a
=> [:a, :a, :b, :c, nil, :e, :a, :b, :c]

concat, compact, uniq恮恆恔态ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰ćÆconcat恮ćæćŖ恮恧态ēµęžœć€å…ƒć®é…åˆ—ać«å½±éŸæć‚’åŠć¼ć™ć®ćÆconcat恮ćæ

concat ē “壊ēš„怀= push = <<

é…åˆ—ć®ęœ«å°¾ć«å¼•ę•°ć‚’ēµåˆć€‚ę„Ÿå˜†ē¬¦ćŒē„”恄ē “壊ēš„ćƒ”ć‚½ćƒƒćƒ‰

conpact 非ē “壊ēš„

é…åˆ—ć‹ć‚‰nilć‚’å–ć‚Šé™¤ć„ćŸć€ę–°ć—ć„é…åˆ—ć‚’ä½œęˆę„Ÿå˜†ē¬¦ćŒć¤ć„ćŸconpact!恌ē “壊ēš„怂

uniq 非ē “壊ēš„

é…åˆ—ć‹ć‚‰é‡č¤‡ć—ćŸč¦ē“ ć‚’å–ć‚Šé™¤ć„ć¦ć€ę–°ć—ć„é…åˆ—ć‚’ä½œęˆę„Ÿå˜†ē¬¦ćŒä»˜ććØē “壊ēš„恫怂

arr = [1, 2, 5, 5, 1, 3, 1, 2, 4, 3]
p arr.uniq
[1, 2, 5, 3, 4]

map = collect

a = [1, 2, 3, 4, 5, 6]

a.collect {|v| v * 2}
=> [2, 4, 6, 8, 10, 12]
a.inject {|v| v * 2}
=> 32
a.each {|v| v * 2}
=> [1, 2, 3, 4, 5, 6]
a.map {|v| v * 2}
=>[2, 4, 6, 8, 10, 12]
a.select {|v| v * 2}
=> [1, 2, 3, 4, 5, 6]
a.execute {|v| v * 2}
=> error  undefined method

ć©ć†ć—ćŸć‚‰inject恌32恫ćŖ悋恋态ćƒŖćƒ•ć‚”ćƒ¬ćƒ³ć‚¹č¦‹ć¦ć‚‚ē†č§£ć§ććŖ恄

%wčØ˜ę³•

é…åˆ—ć‚’ä½œć‚‹

sarray = %w(Apple Orange Grape)
sarray.each {|v| print v, " "}
#
Apple Orange Grape

zip

å®Ÿč”Œēµęžœć«ćŖć‚‹ć‚ˆć†ć«ć€##AAA##恫čؘčæ°ć™ć‚‹ć‚³ćƒ¼ćƒ‰ć‚’éø悓恧äø‹ć•ć„

a = ["a", "b", "c"]
b = [1, 2, 3]
##AAA##

#å®Ÿč”Œēµęžœ
["a", 1]
["b", 2]
["c", 3]

#éøęŠžč‚¢
a.zip(b).each{|x| p x }
a.zip(b){|x| p x }
[a, b].zip{|x, y| p [x, y] }
[a, b].transpose.each{|x, y| p [x, y] }

#ē­”ćˆć€€1ļ¼Œ2ļ¼Œ4

zip

č”ŒćØåˆ—ć‚’å…„ć‚Œę›æćˆć‚‹ć€‚č¶³ć‚ŠćŖ恄要ē“ ćÆnil悒čæ”恙

transpose

zipćØåŒę§˜ć€č”ŒćØåˆ—ć‚’å…„ć‚Œę›æćˆć‚‹ć€‚č¦ē“ ćŒč¶³ć‚ŠćŖć„å “åˆć€ä¾‹å¤–IndexError悒ē™ŗē”Ÿć€‚

EOB

仄äø‹ć®ć‚³ćƒ¼ćƒ‰ćÆę­£ć—ćå‹•ćć¾ć›ć‚“ć€‚äæ®ę­£ę”ˆć‚’怂

s = <<EOB
Hello, Ruby World.
Hi, Ruby World.
Goodbye, Ruby World.
  EOB

#č§£ē­”
5č”Œē›®ć®EOBć®å‰ć®ē©ŗē™½ć‚’å‰Šé™¤ć™ć‚‹ć€‚

encoding

puts "hello".encoding.name
UTF-8
=> nil

chop 非ē “壊ēš„

ę–‡å­—åˆ—ć®ęœ«å°¾ć‹ć‚‰1ę–‡å­—ć‚’å–ć‚Šé™¤ć„ćŸę–°ć—ć„ę–‡å­—åˆ—ć‚’čæ”恙怂!ćŒä»˜ććØ态ē “壊ēš„

chomp 非ē “壊ēš„

ę–‡å­—åˆ—ć®ęœ«å°¾ć®ę”¹č”Œę–‡å­—ć‚’å–ć‚Šé™¤ć„ćŸę–°ć—ć„ę–‡å­—åˆ—ć‚’čæ”恙怂ļ¼ćŒä»˜ććØ态ē “壊ēš„

^ ć‚­ćƒ£ćƒ¬ćƒƒćƒˆ

åć‚’ć‚­ćƒ£ćƒ¬ćƒƒćƒˆć€å¦å®šć‚’ć™ć‚‹

puts "0123456789-".delete("^13-56-")
=> 13456-

13456-ć«č©²å½“ć€Žć—ćŖć„ć€ć‚‚ć®ć‚’å‰Šé™¤ć—ć¦ć„ć‚‹

ę­£č¦č”Øē¾

p "abc def 123 ghi 456".scan(/\d+/).length
=> 2

\dćÆ10é€²ę•°ćŖ恮恧态10é€²ę•°ć‹ć‚‰å§‹ć¾ć‚‹éƒØåˆ†ć‚’é…åˆ—ć§čæ”恗(scan)ć€ćć®č¦ē“ ć®ę•°length悒čæ”恗恦悋

scan(string)

ęŒ‡å®šć—ćŸę­£č¦č”Øē¾ć®ćƒ‘ć‚æćƒ¼ćƒ³ćØ惞惃惁恙悋éƒØåˆ†ć‚’ę–‡å­—åˆ—ć‹ć‚‰ć™ć¹ć¦å–ć‚Šå‡ŗć—ć€é…åˆ—ć«ć—ć¦čæ”恙怂

p "HogeHOGEhoge"[/[A-Z][^A-Z]+/]
=> "Hoge"

å¤§ę–‡å­—ćØå°ę–‡å­—ć®ć‚¢ćƒ«ćƒ•ć‚”ćƒ™ćƒƒćƒˆć§ę§‹ęˆć•ć‚Œć‚‹ę–‡å­—åˆ—ć‚’čæ”恗恦恄悋

invert(hash)

hashć®ć‚­ćƒ¼ćØå€¤ć‚’å…„ć‚Œę›æ恈悋

h = {1 => "Hoge", 2=> "Piyo", 3=>"fuga"}
p h.invert
#
{ā€œHogeā€=>1, ā€œPiyoā€=>2,ā€œfugaā€=>3 }

update ćØ怀sort

a = {"Foo" => "Hoge", "Bar" => "Piyo", "Baz" => "Fuga"}
b = {"Foo" => "hoge", "Bar" => "piyo", "Baz" => "fuga"}
p a.update(b).sort{ |a, b| a[1] <=> b[1] }

[["Baz", "fuga"], ["Foo", "hoge"], ["Bar", "piyo"]]

update

ć‚ć‹ć‚Šć„ć‚‰ć„

sort

é…åˆ—ć®č¦ē“ ć‚’ć‚½ćƒ¼ćƒˆć—ćŸę–°ć—ć„é…åˆ—ć‚’čæ”ć—ć¾ć™ć€‚ć€Œč¦ē“ 1 <=> 要ē“ 2ć€ć®ēµęžœćŒ-1ćŖ悉要ē“ 1ćŒå…ˆć€0ćŖć‚‰åŒć˜ć€1ćŖ悉要ē“ 2ćŒå…ˆćØćŖć‚Šć¾ć™ć€‚

ćƒ«ćƒ¼ćƒˆ

仄äø‹ć®ć‚³ćƒ¼ćƒ‰ćÆć€ćƒ•ć‚”ć‚¤ćƒ« test . txt 悒čŖ­ćæć€ę–‡å­—ć‚’é€†é †ć«ę›øćč¾¼ć‚€å‡¦ē†ć§ć™ć€‚ć€Œ##AAA##ć€ć«å…„ć‚‹é©åˆ‡ćŖčؘčæ°ć‚’éøć³ćŖ恕恄怂

open("test.txt","##AAA##") do |f|
  data = f.read.chomp
  data.reverse!
  f.rewind
  f.write data
end
#éøęŠžč‚¢
a怀怀w怀怀r怀怀a+怀怀r+怀怀w+

ēµ„ćæč¾¼ćæé–¢ę•°open恮ē¬¬2å¼•ę•°ć«ćÆć€ćƒ•ć‚”ć‚¤ćƒ«ć®ć‚Ŗćƒ¼ćƒ—ćƒ³ ćƒ¢ćƒ¼ćƒ‰ć‚’ęŒ‡å®šć—ć¾ć™ć€‚

  • r态w态a 恫 + ć‚’ä»˜ć‘ć‚‹ćØ态čŖ­ćæę›ø恍 äø”ē”Ø恧ć‚Ŗćƒ¼ćƒ—ćƒ³ć—ć¾ć™ć€‚
  • w+ ć‚’ęŒ‡å®šć™ć‚‹ćØå…ƒćƒ•ć‚”ć‚¤ćƒ«ć®å†… å®¹ć‚’ē©ŗć«ć—ć¾ć™ć€‚
  • a+ ć‚’ęŒ‡å®šć™ć‚‹ćØ态čæ½čØ˜ćƒ¢ćƒ¼ćƒ‰ćØćŖ悊态 å…ƒć®ćƒ•ć‚”ć‚¤ćƒ«ć‚’ę›ø恍ꏛ恈悋恓ćØćŒć§ćć¾ć›ć‚“ć€‚

chr ćØ ord ćÆåƾē¾©čŖžēš„

puts 65.chr   => "A"
puts "a".ord  => 97

ć‚³ćƒ¼ćƒ‰ćƒć‚¤ćƒ³ćƒˆć®č©±ć€‚n.chrćÆ,n恧ē™»éŒ²ć•ć‚Œć¦ć„ć‚‹ę–‡å­—ć‚’čæ”恙怂string.ordćÆstringć®ć‚³ćƒ¼ćƒ‰ćƒć‚¤ćƒ³ćƒˆć‚’čæ”ć™ć€‚ćƒ”ć‚½ćƒƒćƒ‰ć®ę„å‘³ćÆč¦šćˆć¦ć‚‹ć‘ć©ć€ć‚‚ć†ć€Ž65ć‚’č¦‹ćŸć‚‰A怏ćØč¦šćˆć‚‹ć—ć‹ć€‚

securerandom Module

安å…ØćŖä¹±ę•°ē™ŗē”Ÿå™Øć®ćŸć‚ć®ć‚¤ćƒ³ć‚æćƒ¼ćƒ•ć‚§ćƒ¼ć‚¹ć‚’ęä¾›ć™ć‚‹ćƒ¢ć‚øćƒ„ćƒ¼ćƒ«

require 'securerandom'
SecureRandom.urlsafe_base64

urlsafe_base64

ćƒ©ćƒ³ćƒ€ćƒ ć§ URL-safe ćŖ base64 ę–‡å­—åˆ—ć‚’ē”Ÿęˆć—恦čæ”ć—ć¾ć™ć€‚