Class: Greeter

Inherits:
Object
  • Object
show all
Defined in:
lib/hello_world_ramziddin/greeter.rb

Overview

The main Greeter class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language = "English") ⇒ Greeter

Returns a new instance of Greeter.



5
6
7
8
9
10
11
12
# File 'lib/hello_world_ramziddin/greeter.rb', line 5

def initialize(language="English")
  @language = language
  @words = {
    "English" => "Hello",
    "Russian" => "Привет",
    "Uzbek" => "Salom"
  }
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



3
4
5
# File 'lib/hello_world_ramziddin/greeter.rb', line 3

def language
  @language
end

Instance Method Details

#add_language(language, greeting) ⇒ Object

Add a new language and a word

Example:

>> greeter = Greeter.new
>> greeter.add_language("Spanish", "Hola")
>> greeter.greet("Spanish")
=> "Hola"


46
47
48
# File 'lib/hello_world_ramziddin/greeter.rb', line 46

def add_language(language, greeting)
  @words[language] = greeting
end

#each(&block) ⇒ Object

Iterates over each word

Example:

>> greeter = Greeter.new
>> greeter.each {|lang, word| greeter.greet(lang) }
=> ["English", "Russian", "Uzbek"]


56
57
58
# File 'lib/hello_world_ramziddin/greeter.rb', line 56

def each(&block)
  @words.each(&block)
end

#greet(language = nil) ⇒ Object

Multi-lang greet

Example:

>> greeter = Greeter.new
>> greeter.greet
=> "Hello" # "English" as default language
>> # or
>> greeter.greet("Uzbek")
=> "Salom"


23
24
25
# File 'lib/hello_world_ramziddin/greeter.rb', line 23

def greet(language=nil)
  @words[language || @language]
end