This file contains 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
# coding: utf-8 | |
class Foo | |
end | |
foo = Foo.new | |
bar = Foo.new | |
# barだけにメソッドを定義する | |
def bar.singleton_method | |
puts 'Called!' |
This file contains 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 OriginalClass | |
end | |
obj = OriginalClass.new | |
def obj.new_singleton_method | |
:new_singleton_method | |
end | |
obj.class.method_defined? :new_singleton_method # => false |
This file contains 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
module ExtendedModule | |
def extend_method | |
:extend_method | |
end | |
end | |
obj = Object.new | |
obj.extend ExtendedModule | |
obj.extend_method # => :extend_method |
This file contains 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 LoggingInstanceVariable | |
logging_instance_val_names = %w(first_val second_val third_val) | |
logging_instance_val_names.each do |val_name| | |
eval <<-END_OF_DEF | |
attr_reader :#{val_name}, :before_#{val_name} | |
def #{val_name}=(val) | |
@before_#{val_name} = @#{val_name} | |
@#{val_name} = val |
This file contains 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 AttrClass | |
def initialize | |
@attr = "attr" | |
end | |
def add_reader(instance_val_name) | |
eval <<-END_OF_DEF | |
def #{instance_val_name} | |
@#{instance_val_name} | |
end |
This file contains 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 EvalTarget | |
def initialize | |
@instance_val = 'instance valiable' | |
end | |
def instance_binding | |
local_val = 'local valiable' # !> assigned but unused variable - local_val | |
binding | |
end |
This file contains 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
# coding: utf-8 | |
def create_proc | |
str = 'from create_proc' | |
Proc.new { str } | |
end | |
proc_obj = create_proc | |
str = 'from toplevel' | |
# proc_obj内で呼び出されるstrの参照はcreate_proc内のstrになっている |
This file contains 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
# coding: utf-8 | |
require 'csv' | |
data = <<EOS | |
name,age,gender | |
alice,14,female | |
bob,32,male | |
carol,17,female | |
EOS |
This file contains 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
require 'csv' | |
converter = ->(field,field_info) { | |
if field_info.index == 1 | |
field.upcase rescue field | |
else | |
field | |
end | |
} |
This file contains 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
require 'csv' | |
headers = %w(name age gender) | |
people = [ | |
['alice',14,'female'], | |
['bob',32,'unknown'], | |
['carol',17,'female'] | |
] | |
csv_string = CSV.generate('', write_headers: true, headers: headers) { |csv| |