Photo by Valeriia Bugaiova on Unsplash

Sorting Out Feeling(s) with Ruby

Showing myself out.
Ruby + Me
class Feeler
attr_accessor :name
@@all = []def initialize(name)
@name = name
@@all << self
end
def self.all
@@all
end
end
class Feeling
attr_accessor :feeler, :name
@@all = []def initialize(feeler, name)
@feeler = feeler
@name = name
@@all << self
end
def self.all
@@all
end
end
> jake = Feeler.new("Jake")
=> #<Feeler:0x00007fb3541cf2a0 @name="Jake">
class Feeler  # ^^^Other Methods^^^  def new_feeling(feeling)
Feeling.new(self, feeling)
end
end
> jake.new_feeling("Excitement")
=> #<Feeling:0x00007fbea4954010
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Excitement">
> jake.new_feeling("Anxiety")
=> #<Feeling:0x00007fb356076a50
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Anxiety">
class Feeler  # ^^^Other Methods^^^  def feelings
Feeling.all.select do |feeling|
feeling.feeler.name == self.name
end
end
end
> jake.feelings
=> [#<Feeling:0x00007fb354114ea0
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Excitement">,
#<Feeling:0x00007fb356076a50
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Anxiety">]
class Feeler# ^^^Other Methods^^^  def feeling_names
self.feelings.map do |feeling|
feeling.name
end
end
end
> jake.feeling_names
=> ["Excitement", "Anxiety"]
class Feeler# ^^^Other Methods^^^  def how_are_you_feeling?(feeling)
"I'm feeling #{feeling}."
end
end
> jake.how_are_you_feeling?("Excitement")
=> "I'm feeling Excitement.
class Feeler# ^^^Other Methods^^^  def how_are_you_feeling?(feeling = "I'm not sure")
"I'm feeling #{feeling}."
end
end
> jake.how_are_you_feeling?
=> "I'm not sure how I'm feeling."
class Feeler# ^^^Other Methods^^^  def how_are_you_feeling?(feeling = "I'm not sure")
if self.feeling_names.include?(feeling.capitalize)
"I'm feeling #{feeling.downcase}."
elsif feeling == "I'm not sure"
"#{feeling} how I'm feeling."
else (defined?(feeling) == nil)
self.new_feeling(feeling.capitalize)
"I'm feeling #{feeling.downcase}...this is new for me."
end
end
end
> jake.how_are_you_feeling?
=> "I'm not sure how I'm feeling."
> jake.how_are_you_feeling?("Excitement")
=> "I'm feeling excitement."
> jake.how_are_you_feeling?("triumph")
=> "I'm feeling triumph...this is new for me."
> jake.feelings
=> [#<Feeling:0x00007fb354114ea0
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Excitement">,
#<Feeling:0x00007fb356076a50
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Anxiety">,
#<Feeling:0x00007fb351010e38
@feeler=#<Feeler:0x00007fb3541cf2a0 @name="Jake">,
@name="Triumph">]
> jake.feeling_names
=> ["Excitement", "Anxiety", "Triumph"]

Software Engineer hailing from the Empire State, writing about what interests me and hoping someone else finds it interesting too. 👨🏻‍💻 🤓 He/Him #LFGM

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store