Ruby Set replace method

In ruby programming language, replace() set method are used to modify set by given enumerable object.

replace(enum) -> self

Ruby replace method example

require "set"
# Set s1
s1 = Set[10,20]
s2 = Set[true,false]

# Display result
print("Before")
print("\ns1  : ",s1.to_a)
print("\ns2  : ",s2.to_a)

# Replace or update set by enumerable object
r1 = s1.replace([3,5])
r2 = s2.replace([10,s1])

print("\nAfter")
# Display result
print("\ns1  : ",s1.to_a)
print("\ns2  : ",s2.to_a)
print("\nr1  : ",r1.to_a)
print("\nr2  : ",r2.to_a)
Before
s1  : [10, 20]
s2  : [true, false]
After
s1  : [3, 5]
s2  : [10, #<Set: {3, 5}>]
r1  : [3, 5]
r2  : [10, #<Set: {3, 5}>]
Ruby replace set method example 1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved