LoginSignup
2
2

More than 5 years have passed since last update.

20150515 Ruby講義memo

Posted at

昨日の補足

succすごい

"test001Z".succ == "test002A"

質問

  • 多態性がよくわからなかった
  • クラスとインスタンスの違い?

デバッグするときに pry-byebug が便利

biding.pry

デザインパターン

古い
http://www.amazon.co.jp/Ruby%E3%81%AB%E3%82%88%E3%82%8B%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3-Russ-Olsen/dp/4894712857
http://orangain.hatenablog.com/entry/design-patterns-in-ruby
http://blue1st.hateblo.jp/entry/2014/10/12/014015

Effective Rubyがいいよ
http://www.amazon.co.jp/Effective-Ruby-Peter-J-Jones/dp/4798139823/ref=sr_1_1?s=books&ie=UTF8&qid=1431650604&sr=1-1&keywords=Effective+Ruby

http://tbpgr.hatenablog.com/entry/20140515/1400160516
http://morizyun.github.io/blog/ruby-design-pattern-matome-mokuzi/
http://qiita.com/kidach1/items/4b63de9ad5a97726c50c

Block

def block_args_test
  yield()
  yield(1)
  yield(1, 2, 3)
  yield(x:1, y:2)
end

block_args_test do |a|
  p [a]
end

puts
block_args_test do |a,b,c|
  p [a,b,c]
end

puts
block_args_test do |*a|
  p [a]
end

ブロック内の break 戻り値ありもできる。
ブロックをオブジェクトとして受け取る

数値

配列

  • index付の配列
  • 集合として
  • 列として (cue,stack)
a = [1,2,3,4,5]
a[0,2] = [10,11,12] # 要素の置換
p a

a[0,0] = [101,102]  # 要素の挿入
p a

# 集合として
a1 = [1,2,3]
a2 = [3,2,5]
p a1 - a2
p a1 + a2
p a1 | a2

String

p Encoding.name_list

str = "あたらしいString"
puts str[2..3]
p str.bytesize

# printf相当がこれで書ける
p "%04d" % 123
p "%04d + %05d" % [123,44]

Hash


#最新バージョンでのハッシュは
p "a".hash
p :a.hash

Rubyによるデザインパターン いい本だな。
この知見は共有したい。

Hash

#最新バージョンでのハッシュは
p "a".hash
p :a.hash

s = Regexp.quote("abc*def+")
p s

$stdout.puts "output to $stdout"
$stderr.puts "output to $stderr"

p "abacdeafg".scan(/.a/)

s = "Ruby is Language"
p s[/Ru.*\s+/]

m = /(.)(\d\d)+(.)/.match "12345"
p m
p $1
p $2
p $3

IO

Time


require 'open-uri'
stringio

t = Time.parse("S54.3.4")
p t
now_t = Time.now

p t_r = (t..now_t)
p t_r.cover?( Time.parse("H20.4.1") )
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2