LoginSignup
1
2

More than 5 years have passed since last update.

ruby

Last updated at Posted at 2018-11-05

予約語

変数名の命名

Time

t = Time.now + (60*60*24)
p Time.now
p (60*60*24)
p t


出力
2018-11-05 13:57:39 +0900
86400
2018-11-06 13:57:39 +0900


24時間後(86400秒後)
範囲&each_with_index

(5..8).each_with_index do |val,i|
puts "#{i} #{val}"
end

dsdsss

(5..8)
5,6,7,858は入る

結果

0 5
1 6
2 7
3 8

downto&select

puts 100.downto(90)

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

p a.select{|x| x % 2 == 0}

p 100.downto(90).select{|x| x%2==0}


出力

[2, 4, 56, 6, 6, 6, 6]
[100, 98, 96, 94, 92, 90]

目的

10090で、偶数のモノだけ取り出したい

delete

puts "Ruby on Rails".delete("Rails")

結果

uby on

("Rails")ここに入っている文字列と合致するものが、削除される

file

File.open("foo.txt","r") do |io|
puts io.gets
 puts io.read
  io.rewind
  p lines = io.readlines
end

結果

dadasd
sadas




["dadasd\n", "sadas\n", "\n", "\n", "\n", "\n"]

readは一行ごとに読む

File.open("foo.txt","r") do |io|
 puts io.read

end


結果
1dadasd
2sadas
3
4
5
6


rewind

ファイルの先頭に戻す

配列の足し算&sort

odd = [1,3,5]
even = [2,4,6]
num = odd + even
p num.sort


結果
[1, 2, 3, 4, 5, 6]

正規表現
strings =["Hello","holland","Cello","h35L320"]

strings.each do |st|
 puts st =~ /^[hc].*o$/i
end

結果

0

0


File$join

p File.join("ruby", "exam","silver")

結果

"ruby/exam/silver"


class&instance&super&attr_reader

class Surface
  attr_reader :s
  def initialize(x,y)
   @s = x * y
  end
end

class Volume < Surface
  attr_reader :v
  def initialize(x,y,z)
    super(x,y)
   @v = x * y * z
  end
end

a = Volume.new(2,5,5)
puts "#{a.v},#{a.s}"

結果

50,10

||の使い方

puts ary = []
puts ary << 1 && false
puts true || ary << 2
puts false && ary << 3
puts false || ary << 4
p ary


結果

false
true
false
1
4
[1, 4]
計算

p "foo" * 2 **2


結果

(foo x 2)2

"foofoofoofoo"

dup

foo = [1,2,3]
bar = foo
baz = foo.dup

bar[3] = 4
p foo
p bar
p baz

結果

[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 3]
正規表現

str = ["0x000000", "0-93-0-94", "93094", "333-4421" ]

str.each do |s|
  puts s

  puts s !~ /[0-9]{3}-[0-9]{4}/
end


結果

0x000000
true
0-93-0-94
true
93094
true
333-4421
false


用語

メタ文字: {3} 
 → 文字を繰り返す回数
判定:!~   
→ マッチしたモノをfalseで返す

否定文字:^(ハット)
→ どの文字にもマッチしないというパターンを作成

範囲: -
→ str.match(/[a-z]pple/)

文字列の先頭末尾など

 ^  行頭 
 $  行末 
 \A  文字列の先頭 
 \z  文字列の末尾 
 \Z  文字列の末尾(末尾が改行文字ならばその手前にマッチ


記事:
https://qiita.com/shizuma/items/4279104026964f1efca6

正規表現

puts "0123456789".delete("0-58-")

結果

679

メモ
05 
8
を削除

inject

numbers = [3,89,40,39,29,10,50,59,69]
num = numbers.inject do |i,j|
  i > j ? i : j
end
p num

結果

89

記事:https://ref.xaio.jp/ruby/classes/enumerable/inject
正規表現

p String.instance_methods.grep(/strip/)

結果:

[:rstrip, :strip!, :rstrip!, :strip, :lstrip!, :lstrip]

uri

___(1)___ 'uri'
uri = URI::HTTP.build({host:'www.ruby.or.jp', path:'/ja/certification/examination/'})
puts uri

結果
http://www.ruby.or.jp/ja/certification/examination/
unless&||

1.false
unless false || nil
  print "Hello\n"
end

結果
Hello

2.true
unless true || nil
  print "Hello\n"
end

結果
何もなし

nilはなんと評価されているのか?

detect&find

numbers = (1..20).to_a
p numbers.detect{|x| x % 5 == 0}

結果
5
該当のものがあると止まる

例外処理

class ExceptionTest
  def test
    begin
      # 0での除算でエラーを発生させる
      1/0
    rescue ZeroDivisionError => ex
      puts "ZeroDivisionError"
    end
  end
end

obj = ExceptionTest.new
# 例外発生

結果
obj.test # => ZeroDivisionError


記事:
https://qiita.com/murata0705/items/6ae346bd04197fb2e36a

解答みる

問題8
問題11.
問題12
問題16.
問題26
問題28.
問題40.


オブジェクトid

a = "foo"

puts a.object_id


→ 70233574692360

インスタンス変数へのアクセス

class Foo
  def initialize
   @hoge = 1
  end

end


f = Foo.new
puts f

puts f.instance_variables
puts f.instance_variable_get(:@hoge)

 → 1
puts f.instance_variable_set(:@hoge, 2)
→ 2
puts f.instance_variable_get(:@hoge)
→ 2
1
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
1
2