Class: LocalizedString

Inherits:
String
  • Object
show all
Defined in:
lib/ext/localized_string.rb

Overview

LocalizedString is just a common String with “lang” attribute, which specifies the language of the text stored in the string.

Examples:

str = LocalizedString.new("Hello", :en)
"Bonjour".with_lang(:fr) == str # => false

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#with_lang

Constructor Details

#initialize(str, lng = nil) ⇒ LocalizedString

Returns a new instance of LocalizedString.



10
11
12
13
# File 'lib/ext/localized_string.rb', line 10

def initialize(str, lng = nil)
  @lang = lng
  super str
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



8
9
10
# File 'lib/ext/localized_string.rb', line 8

def lang
  @lang
end

Instance Method Details

#eql?(str) ⇒ Boolean Also known as: ==

In addition to comparing string values LocalizedString also compares “lang” values. When compared to a common String, “lang” value is ignored.

Parameters:

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/ext/localized_string.rb', line 22

def eql?(str)
  if str.respond_to?(:lang)
    self.lang == str.lang && super
  else
    super
  end
end