Global 'include <Some_Module>' conflicts with OpenStruct

A

Alexey Petrushin

Hello!

I need to include methods from module 'A' into global scope. But i have
faced problem, it does interfere with OpenStruct.
The Code:
# ------------------------------
require 'ostruct'

module A
def text; puts "The 'text' method from the 'A' module." end
end
include A

os = OpenStruct.new:)text => "Text from the 'OpenStruct'")

puts os.text #=> The 'text' method from the 'A' module. \n nil
# ------------------------------
How it can be solved?

Desired behavior should be:
puts os.text #=> Text from the 'OpenStruct'

Thanks!
 
R

Ryan Davis

Hello!

I need to include methods from module 'A' into global scope. But i
have
faced problem, it does interfere with OpenStruct.
The Code:
# ------------------------------
require 'ostruct'

module A
def text; puts "The 'text' method from the 'A' module." end
end
include A

os = OpenStruct.new:)text => "Text from the 'OpenStruct'")

puts os.text #=> The 'text' method from the 'A' module. \n nil
# ------------------------------
How it can be solved?

Desired behavior should be:
puts os.text #=> Text from the 'OpenStruct'

OpenStruct works via method_missing... if the method isn't missing, it
isn't missing... You'd probably have to add an explicit method on your
os object to override the Kernel#text. Looking at the source, that
appears to be what #new_ostruct_member is for.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top