Menggunakan Instance Eval Untuk Implementasi Ruby Block
- Categories:
- ruby
Artikel ini dibuat berdasarkan pertanyaan pada StackOverflow mengenai implementasi Ruby block. Pada saat implementasi Ruby block, lebih baik menggunakan instance_eval
dibandingkan dengan yield
jika kita ingin Ruby block dikirimkan tanpa parameter seperti dibawah ini.
Menggunakan yield
class MyClass
def initialize
@my_array = []
end
def elements
block_given? ? yield(self) : @my_array
end
def add_element( text )
@my_array << text
end
end
# error
my_object = MyClass.new
my_object.elements {
add_element "hello"
add_element "world"
} # => NoMethodError: undefined method `add_element' for main:Object
p my_object.elements # => []
# ok
my_object = MyClass.new
my_object.elements { |a|
a.add_element "hello"
a.add_element "world"
}
p my_object.elements # => ["hello", "world"]
Menggunakan instance_eval
class MyClass
def initialize
@my_array = []
end
def elements(&block)
block_given? ? instance_eval(&block) : @my_array
end
def add_element( text )
@my_array << text
end
end
# ok, no error
my_object = MyClass.new
my_object.elements {
add_element "hello"
add_element "world"
}
p my_object.elements # => ["hello", "world"]
# ok too
my_object = MyClass.new
my_object.elements { |a|
a.add_element "hello"
a.add_element "world"
}
p my_object.elements # => ["hello", "world"]
- Tags:
- #ruby
Recent Posts
C# DbContext ServiceLifeTime
my note about C Sharp ServiceLifeTime
PostgreSQL Index Usage Monitoring
Having too many unused or underused indexes on a table can slow down write and update operations in your PostgreSQL database, making it crucial to regularly identify and manage them for optimal performance.
KAK Labs Newsletter #6 - Staying Safe From Pegasus Spyware
Newsletter #6 - Pegasus, Ruby, PostgreSQL and networkQuality tool
Material Design - Paragraph Spacing
According to Google's Material Design, keep paragraph spacing in the range between .75x and 1.25x of the type size.
Amazon SDK for C# - S3 File Download Methods
Comparison between `TransferUtility.DownloadAsync`, `DownloadSingleFileAsync`, and `GetObjectAsync`.