7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyチュートリアル学習メモ

Last updated at Posted at 2016-04-14

概要

  • ドットインストール:Ruby入門
  • 基礎文法の学習
  • オブジェクト指向スクリプト言語
  • 公式サイト
    https://www.ruby-lang.org/ja/

Hello World

hello.rb
=begin
stdout Hello World
=end
print "hello world";
puts "hello world(puts)";
p "hello world(p)";

変数と定数

hello.rb
=begin
ローカル変数
定数
=end

HOGE = "HELLO WORLD";
def sample_func
    hoge = "hello world";
    p hoge;

    p HOGE;
end

# run
sample_func

文字列オブジェクト

hello.rb
=begin
String Class
length Method
=end

HOGE = "HELLO WORLD";
def sample_func
    hoge = "hello world";
    p hoge;
    p hoge.length;

    name = "hoge";
    x = "hello \tworld\n #{name}"; # 特殊文字、変数展開
    y = 'hello \tworld\n #{name}';

    puts x;
    puts y;

    puts "hello world " + "momo";
    puts "hello " * 5;

end

# run
sample_func

$ruby hello.rb
"hello world"
11
hello world
hoge
hello \tworld\n #{name}
hello world momo
hello hello hello hello hello

数値オブジェクト

hello.rb
=begin
Numeric Class
=end
def sample_func
    x = 10; # 100_000_000
    y = 20.5
    z = 1/3r #Rational(1,3)

    p x % 3;  # 1
    p x ** 3; # 1000
    p z * 2;  # 2/3

    x += 5; # 自己代入
    p x;

    p y.round;
end

sample_func

1
1000
(2/3)
15
21

破壊的メソッド, 真偽値返却メソッド

hello.rb
=begin
Method
 ! 破壊的メソッド
 ? 真偽値返却メソッド
=end
def sample_func
    s = "hogehoge";
    puts s.upcase;
    puts s;

    puts s.upcase!;
    puts s;

    ss = ""; 
    puts s.empty?;
    puts ss.empty?;

end

sample_func


HOGEHOGE
hogehoge
HOGEHOGE
HOGEHOGE
false
true

配列オブジェクト

hello.rb
=begin
 Array
=end

def sample_func
    array = [10, 20, 30, 40, 50];
    array[2] = 31; 

    p array[2];
    p array[0..2];
    p array[0...2];
    p array[-1];
    p array[1, 2]; 

    array[0...2] = [1, 2]; 
    p array;

    array[1, 0] = [10, 11, 12];
    p array;

    array[0, 2] = []; 
    p array;

    p array.size;
    p array.sort;
    p array.sort.reverse;
    p array.push(100);

    array << 200 << 300;
    p array;
end

sample_func

$ruby hello.rb
31
[10, 20, 31]
[10, 20]
50
[20, 31]
[1, 2, 31, 40, 50]
[1, 10, 11, 12, 2, 31, 40, 50]
[11, 12, 2, 31, 40, 50]
6
[2, 11, 12, 31, 40, 50]
[50, 40, 31, 12, 11, 2]
[11, 12, 2, 31, 40, 50, 100]
[11, 12, 2, 31, 40, 50, 100, 200, 300]

ハッシュオブジェクト

hello.rb
=begin
 Hash
=end

def sample_func
    hash1 = {"hoge" => 100, "momo" => 200};
    p hash1["hoge"];

    hash2 = {:hoge => 100, :momo => 200};
    p hash2[:hoge];

    hash3 = {hoge:100, momo:200};
    p hash3[:hoge];

    p hash3.size;
    p hash3.keys;
    p hash3.values;
    p hash3.has_key?(:hoge);

end

sample_func

$ruby hello.rb
100
100
100
2
[:hoge, :momo]
[100, 200]
true

##オブジェクト変換

hello.rb
=begin
 to Convert
=end

def sample_func
    x = 10;
    y = "5";
    p x + y.to_i;
    p x + y.to_f;
    p x.to_s + y;

    hash = {hoge:100, momo:200};
    p hash.to_a;
    p hash.to_a.to_h;

    array = [{hoge:100, momo:200}, {huga:300, tete:400}];
    p array;
    p array[0][:hoge];

end

sample_func

$ruby hello.rb
15
15.0
"105"
[[:hoge, 100], [:momo, 200]]
{:hoge=>100, :momo=>200}
[{:hoge=>100, :momo=>200}, {:huga=>300, :tete=>400}]
100

##%記法

hello.rb
=begin
 %記法
=end

def sample_func
    # s = "hello"
    s1 = %(hello);
    s2 = %/hello/;
    s3 = %Q(hello);
    p s1; 

    # s = 'hello'
    s4 = %q(hello);
    p s4; 

    # a = ["a", "b", "c"]
    a1 = %W(a b c);
    p a1; 

    # a = ['a', 'b', 'c']
    a2 = %w(a b c);
    p a2; 
    
    # k = :simble
    k1 = %s(key1);
    p k1;

    # ak = [:simble1, :simble2]
    ak = %i(simble1 simble2);
    p ak;

end

sample_func

$ruby hello.rb
"hello"
"hello"
["a", "b", "c"]
["a", "b", "c"]
:key1
[:simble1, :simble2]

##条件分岐

hello.rb
=begin
 if, case
 true:  下記以外(0, ''含む)
 false:  false nil 
=end

def sample_func
    x = 100;

    if x > 100
        puts "hoge";
    elsif x > 50
        puts "huga";
    else
        puts "tete";
    end

    puts "momo" if x == 100;

    y, z = 10, 20;
    n = x > 80 ? y : z;
    puts n;

    color = "blue";
    case color
    when "red"
        puts "111";
    when "green", "blue"
        puts "222";
    when "yellow"
        puts "333";
    else
        puts "err";
    end
end

sample_func

$ruby hello.rb
huga
momo
10
222

##繰り返し処理

hello.rb
=begin
 times
 while
 for
 each
=end

def sample_func
    5.times do |i| 
        next if i == 1;
        break if i > 3;
        puts "#{i}: hello";
    end 
    puts "------"

    i = 0;
    while i < 5 do
        puts "#{i}: hello";
        i += 1;
    end 
    puts "------"

    for i in 0..5 do
        puts "#{i}: hello";
    end 
    puts "------"

    for color in ["red", "blue", "black"] do
        puts color;
    end 
    puts "------"

    ["red", "blue", "black"].each do |color|
        puts color;
    end 
    puts "------"

    {"red"=>100, "blue"=>200, "black"=>300}.each do |color, value|
        puts "#{color}: #{value}";
    end 
end

sample_func

$ruby hello.rb
0: hello
2: hello
3: hello

0: hello
1: hello
2: hello
3: hello
4: hello

0: hello
1: hello
2: hello
3: hello
4: hello
5: hello

red
blue
black

red
blue
black

red: 100
blue: 200
black: 300

##関数

hello.rb
=begin
 function
=end

def sample_func(val = "hoge")
    s = "hello - " + val;
    return s;
end

ss = sample_func("momo");
puts ss; 

ss = sample_func();
puts ss; 

$ruby hello.rb
hello - momo
hello - hoge

##クラス

hello.rb
=begin
 Class
=end

class User
    @@count = 0;

    def initialize(name)
        @name = name;
        @@count += 1;
    end 

    def hello
        puts "hello - " + @name;
    end 

    def User.staticHello
        puts "hello - static: #{@@count}";
    end 
end

User.staticHello();
hoge = User.new("momo");
hoge.hello();
User.staticHello();

$ruby hello.rb
hello - static: 0
hello - momo
hello - static: 1

##クラス継承、アクセッサー

hello.rb
=begin
 Class
=end

class User

    def initialize(name)
        @name = name;
    end 

    def hello
        puts "hello - #{@name}";
    end 

end

class HogeUser < User
    attr_accessor :rw_item;
    attr_reader :r_item;
    attr_writer :w_item;

    def initialize(name)
        @name = name;
        @r_item = name;
    end 

    def hoge
        puts "hoge hoge -  #{@name} #{@w_item}";
    end 

end

user = User.new("momo");
user.hello();

hoge_user = HogeUser.new("tete");
hoge_user.hello();
hoge_user.hoge();
hoge_user.rw_item  = "huga";
puts hoge_user.rw_item;
puts hoge_user.r_item;
puts hoge_user.w_item = "wwwww";
hoge_user.hoge();

$ruby hello.rb
hello - momo
hello - tete
hoge hoge - tete
huga
tete
wwwww
hoge hoge - tete wwwww

7
7
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
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?