Class: DictionaryRB::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/dictionary-rb/word.rb

Overview

Contains the word and offers methods for Urban and Reference Dictionary Also provides methods like #meaning, #urban_meaning etc for direct access. Lot of precautions has been taken to prevent repeated call on endpoints

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Word

Returns a new instance of Word.

Examples:

word = DictionaryRB::Word.new('boast')

Parameters:

  • word (String)

    The word for Dictionary



19
20
21
# File 'lib/dictionary-rb/word.rb', line 19

def initialize(word)
  @word = word
end

Instance Attribute Details

#dictionaryDictionaryRB::Dictionary (readonly)

Gives Reference Dictionary instance object for word

Examples:

word.dictionary.synonyms

Returns:

See Also:



13
14
15
# File 'lib/dictionary-rb/word.rb', line 13

def dictionary
  @dictionary
end

#urbanDictionaryRB::Urban (readonly)

Gives Urban Dictionary instance object for word

Examples:

word.urban.synonyms

Returns:

See Also:



11
12
13
# File 'lib/dictionary-rb/word.rb', line 11

def urban
  @urban
end

#wordObject (readonly)

The associated word



9
10
11
# File 'lib/dictionary-rb/word.rb', line 9

def word
  @word
end

Instance Method Details

#dictionary_meaningString Also known as: meaning

Note:

This method will hit the ENDPOINT and will consume some time to generate result

Gives dictionary meaning for the word

Examples:

word.dictionary_meaning
#or
word.meaning

Returns:



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dictionary-rb/word.rb', line 39

def dictionary_meaning
  if @dictionary_meaning.nil?
    @dictionary = Dictionary.new(@word)
    meanings = @dictionary.meanings
    if meanings.is_a? Array and not meanings.empty?
      @dictionary_meanings = meanings
      @dictionary_meaning = @dictionary.meaning
    end
  end
  @dictionary_meaning
end

#dictionary_meaningsArray Also known as: meanings

Fetches all meanings from Reference Dictionary for the word. It is greedily evaluated to save computation when #dictionary_meaning is called.

Examples:

word.dictionary_meanings
#or
word.meanings

Returns:

  • (Array)

    array containing the meanings from Reference Dictionary for the word

See Also:



59
60
61
62
63
64
# File 'lib/dictionary-rb/word.rb', line 59

def dictionary_meanings
  if @dictionary_meanings.nil?
    dictionary_meaning
  end
  @dictionary_meanings
end

#to_sObject



110
111
112
# File 'lib/dictionary-rb/word.rb', line 110

def to_s
  sprintf("%s", @word)
end

#urban_meaningString

Note:

This method will hit the ENDPOINT and will consume some time to generate result

Fetches the first meaning from Urban Dictionary for the word.

Examples:

word.urban_meaning

Returns:

  • (String)

    containing meaning from Urban Dictionary for the word

See Also:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dictionary-rb/word.rb', line 81

def urban_meaning
  if @urban_meaning.nil?
    @urban = Urban.new(@word)
    meanings = @urban.meanings
    if meanings.is_a?(Array) and not meanings.empty?
      @urban_meanings = meanings
      @urban_meaning = @urban.meaning
    end
  end
  @urban_meaning
end

#urban_meaningsArray

Fetches all meanings from the Urban Dictionary for the word. It is greedily evaluated when #urban_meaning is called

Examples:

word.urban_meanings
#=> ['brag', 'lie', 'boaster']

Returns:

  • (Array)

    array containing meanings from Urban Dictionary for the word

See Also:



100
101
102
103
104
105
# File 'lib/dictionary-rb/word.rb', line 100

def urban_meanings
  if @urban_meanings.nil?
    urban_meaning
  end
  @urban_meanings
end