This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hello(names) | |
names.each do |name| | |
puts "Hello, #{name.upcase}" | |
end | |
end | |
rubies = ['MRI','jruby'] | |
hello(rubies) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#定数は大文字アルファベットで始まる | |
FOO_BAR = 'bar' | |
#再代入はおk | |
FOO_BAR = 'bar2' | |
puts FOO_BAR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = 2 | |
if n.zero? | |
puts "0でした" | |
elsif n.even? | |
puts '偶数' | |
elsif n.odd? | |
puts '奇数ですた' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ruler | |
#rubyではgetterに=をつける習慣がある。というかシンタックスシュガーになっている | |
def length=(val) | |
@length = val | |
end | |
def length | |
@length | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ruler | |
#自動的にgetter setterが実装される | |
attr_accessor :length | |
def display_length | |
puts length | |
end | |
end | |
ruler = Ruler.new |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ruler | |
attr_accessor :length | |
#コンストラクタ | |
def initialize(length) | |
@length = length | |
end | |
end | |
ruler = Ruler.new(30) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ruler | |
attr_accessor :length | |
#selfでクラスメソッド | |
def self.pair | |
[Ruler.new,Ruler.new] | |
end | |
end | |
puts Ruler.pair |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass | |
@@cvar = 'Hello, My class variable!' | |
def cvar_in_method | |
puts @@cvar | |
end | |
def self.cvar_in_class_method | |
puts @@cvar | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent | |
def hello | |
puts 'Hello,Parent class!' | |
end | |
end | |
class Child < Parent | |
def hi | |
puts 'Hello, Child class!' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Parent | |
def hello | |
puts 'Hello,Parent class!' | |
end | |
end | |
class Child < Parent | |
def hello | |
super | |
puts 'Hello,Child class!' |