简体   繁体   中英

Using two Ruby classes with same name

I'm using JRuby and working with dates. The framework I'm in forces me to use both JodaTime's DateTime and Ruby's standard DateTime.

How do I handle them having the same name?

How do I refer to one when the other is also required?

You have to keep classes with the same name in different namespaces.

To avoid conflicts with Joda-Time's DateTime , you could assign the Java package to a constant:

require 'date'
require 'java'

JT = org.joda.time

JT::DateTime  # <- Joda-Time's DateTime
DateTime      # <- Ruby's DateTime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM