if condition do
# execute here when condition is true
end
while condtion do
# execute here again and again while condition is true
end
origin
class NameList
def initialize(name, &block)
@name = name
@list = []
end
def add(name, &block)
@list << addition = NameList.new(name)
addition
end
def print
puts to_s
end
def to_s
if @list.empty?
"- " + @name.to_s
else
"[" + @name.to_s + "]\n" + @list.map(&:to_s).join("\n").gsub(/(^)/, " ")
end
end
end
list = NameList.new(:Tsukukoma)
grade_2 = list.add(:grade_2)
class_1 = grade_2.add(:class_1)
class_1.add :Hideshima
class_1.add :Fujishiro
class_2 = grade_2.add(:class_2)
class_2.add :Hideshi
class_2.add :Fujishi
class_3 = grade_2.add(:class_3)
class_3.add :Hide
class_3.add :Fuji
class_3.add :Tsuku
list.print
instance_eval
class NameList
def initialize(name, &block)
@name = name
@list = []
instance_eval(&block) if block
end
def add(name, &block)
@list << addition = NameList.new(name, &block)
addition
end
def print
puts to_s
end
def to_s
if @list.empty?
"- " + @name.to_s
else
"[" + @name.to_s + "]\n" + @list.map(&:to_s).join("\n").gsub(/(^)/, " ")
end
end
end
list = NameList.new :Tsukukoma do
add :grade_2 do
add :class_1 do
add :Hideshima
add :Fujishiro
end
add :class_2 do
add :Hideshi
add :Fujishi
end
add :class_3 do
add :Hide
add :Fuji
add :Tsuku
end
end
end
list.print
method_missing
class NameList
def initialize(name, &block)
@name = name.to_sym
@list = []
instance_eval(&block) if block
end
def add(name, &block)
@list << addition = NameList.new(name, &block)
addition
end
def print
puts to_s
end
def method_missing(name, *args, &block)
raise ArgumentError, "wrong number of arguments (" + args.size.to_s + " for 0)" unless args.empty?
add(name, &block)
end
def to_s
if @list.empty?
"- " + @name.to_s
else
"[" + @name.to_s + "]\n" + @list.map(&:to_s).join("\n").gsub(/(^)/, " ")
end
end
end
list = NameList.new :Tsukukoma do
grade_2 {
class_1 {
hideshima
fujishiro
}
class_2 {
hideshi
fujishi
}
class_3 {
hide
fuji
tsuku
}
}
end
list.print
define_singleton_method
class NameList
def initialize(name, &block)
@name = name
@list = []
instance_eval(&block) if block
end
def add(name, &block)
@list << addition = NameList.new(name, &block)
define_singleton_method(name){ addition } unless respond_to?(name, true)
addition
end
def print
puts to_s
end
def to_s
if @list.empty?
"- " + @name.to_s
else
"[" + @name.to_s + "]\n" + @list.map(&:to_s).join("\n").gsub(/(^)/, " ")
end
end
end
list = NameList.new :Tsukukoma do
add :grade_2 do
add :class_1 do
add :Hideshima
add :Fujishiro
end
add :class_2 do
add :Hideshi
add :Fujishi
end
add :class_3 do
add :Hide
add :Fuji
add :Tsuku
end
end
end
list.print
list.grade_2.print
list.grade_2.class_1.print
delete
class NameList
def initialize(name, &block)
@name = name
@list = []
instance_eval(&block) if block
end
def add(name, &block)
@list << addition = NameList.new(name, &block)
addition
end
def print
puts to_s
end
def to_s
if @list.empty?
"- " + @name.to_s
else
"[" + @name.to_s + "]\n" + @list.map(&:to_s).join("\n").gsub(/(^)/, " ")
end
end
end
list = NameList.new :Tsukukoma do
add :grade_2 do
add :class_1 do
add :Hideshima
add :Fujishiro
end
add :class_2 do
add :Hideshi
add :Fujishi
end
add :class_3 do
add :Hide
add :Fuji
add :Tsuku
end
end
end
list.print
list.instance_eval do
@list.replace []
end
list.print