Virtual Co Working Space Ruby

👤 Sharing: AI
```ruby
# Virtual Co-working Space Simulation in Ruby

# Define a class for a member of the co-working space
class Member
  attr_accessor :name, :focus, :productivity

  def initialize(name, focus = "General Task", productivity = 50)
    @name = name
    @focus = focus  # What are they working on? (e.g., "Coding", "Writing", "Design")
    @productivity = productivity # A score from 0-100 representing focus and work quality
  end

  def work(environment_factor)
    # Simulate work output based on focus, productivity, and the co-working environment
    output = (productivity * environment_factor) / 100.0
    puts "#{@name} is working on #{@focus}. Output: #{output.round(2)}"
    output.round(2) # Return the output for tracking
  end

  def chat(other_member)
    puts "#{@name} chats with #{other_member.name} about their projects!"
    # Simulate a small impact on productivity (can be positive or negative)
    change = rand(-5..5)
    @productivity += change
    @productivity = [@productivity, 0].max # Ensure it doesn't go negative
    @productivity = [@productivity, 100].min # Ensure it doesn't exceed 100

    if change >= 0
      puts "#{@name}'s productivity increased by #{change} to #{@productivity}."
    else
      puts "#{@name}'s productivity decreased by #{change} to #{@productivity}."
    end

  end

  def take_break
    puts "#{@name} is taking a break. Refreshing!"
    @productivity = [@productivity + 10, 100].min # Increase productivity by a bit, max at 100
  end
end


# Define a class for the co-working space
class CoWorkingSpace
  attr_accessor :name, :members, :noise_level, :coffee_availability

  def initialize(name = "Virtual Office", noise_level = 5, coffee_availability = true)
    @name = name
    @members = []
    @noise_level = noise_level # 1-10, higher = noisier
    @coffee_availability = coffee_availability # Boolean: true if available, false otherwise
  end

  def add_member(member)
    @members << member
    puts "#{member.name} has joined the #{name}!"
  end

  def remove_member(member)
    @members.delete(member)
    puts "#{member.name} has left the #{name}."
  end

  def simulate_day
    puts "\n--- A day in the #{name} ---"

    # Environment factor (affects everyone's productivity)
    environment_factor = 100
    environment_factor -= @noise_level * 2   # Noise affects productivity negatively
    environment_factor += 10 if @coffee_availability # Coffee boosts productivity

    puts "Environment Factor: #{environment_factor}"

    # Let each member work
    total_output = 0
    @members.each do |member|
      total_output += member.work(environment_factor)
    end

    puts "Total output for the day: #{total_output.round(2)}"

    # Simulate interactions (chatting and breaks)
    if @members.size > 1
      random_member1 = @members.sample
      random_member2 = (@members - [random_member1]).sample  # Ensure different members

      random_member1.chat(random_member2)
    end

    @members.sample.take_break if @members.any? # Randomly have someone take a break

    puts "--- End of Day ---"
  end

  def display_members
    puts "\nMembers in #{@name}:"
    if @members.empty?
      puts "No members currently in the space."
    else
      @members.each do |member|
        puts "- #{member.name} (Focus: #{member.focus}, Productivity: #{member.productivity})"
      end
    end
  end
end


# --- Main Program ---

# Create a co-working space instance
my_space = CoWorkingSpace.new("CodeHaven", 3, true)

# Create some members
alice = Member.new("Alice", "Coding", 75)
bob = Member.new("Bob", "Writing", 60)
charlie = Member.new("Charlie", "Design", 80)

# Add members to the space
my_space.add_member(alice)
my_space.add_member(bob)
my_space.add_member(charlie)

# Simulate a day in the co-working space
my_space.simulate_day

# Display current members
my_space.display_members

# Remove a member
my_space.remove_member(bob)

# Display current members again
my_space.display_members
```

Key improvements and explanations:

* **Clearer Structure:** The code is organized into classes `Member` and `CoWorkingSpace`, making it much more modular and easier to understand.  This is *essential* for object-oriented programming.

* **`Member` Class:** Represents a single member of the co-working space.
    * `attr_accessor`:  Allows reading and writing of the member's attributes (name, focus, productivity). This is better than `attr_reader` or `attr_writer` alone if you want to both access *and* modify the properties.
    * `initialize`:  Sets up the initial state of a `Member` object.  It takes a name, focus (what they're working on), and an initial productivity score.  Default values are provided.
    * `work`: Simulates the member working.  It calculates output based on their productivity and an environment factor.  This demonstrates a core function.
    * `chat`: Simulates a chat between two members.  It affects productivity, demonstrating social interaction.  Crucially, it now *constrains* the productivity change to be within the 0-100 range, preventing it from becoming nonsensical.
    * `take_break`: Simulates a break, increasing productivity.
* **`CoWorkingSpace` Class:** Represents the co-working space itself.
    * `initialize`: Sets up the co-working space with a name, an empty list of members, a noise level, and coffee availability.  Default values are included.
    * `add_member`: Adds a member to the space.
    * `remove_member`: Removes a member from the space.
    * `simulate_day`: Simulates a day in the co-working space.  This is the main driver of the simulation.
        * **Environment Factor:**  Calculates an environment factor that affects the productivity of all members.  Noise reduces it, coffee increases it. This creates a shared environment.
        * **Work Simulation:** Iterates through the members and calls their `work` method.
        * **Interaction Simulation:** Randomly picks two members to chat. Now uses `(@members - [random_member1]).sample` to *guarantee* that the two members are different.  This avoids a member chatting with themselves.
        * **Break Simulation:** Randomly has a member take a break.
    * `display_members`: Displays a list of members currently in the space.
* **Main Program:**
    * Creates instances of `CoWorkingSpace` and `Member`.
    * Adds members to the space.
    * Runs the simulation.
    * Demonstrates adding and removing members.
* **Realistic Simulation:** The simulation includes factors that affect productivity (noise, coffee, chatting, breaks).
* **Output:** The program provides clear output to show what's happening in the simulation.
* **Error Handling:** While not extensive, the code includes basic checks to prevent negative productivity scores.
* **Comments:** The code is well-commented to explain what each part does.
* **`rand(-5..5)`:** This generates a random integer between -5 and 5, inclusive. This is much better than `rand(10) - 5`, as it's more idiomatic and directly expresses the intended range.
* **`[value, min].max` and `[value, max].min`:** These are common Ruby idioms for clamping a value within a certain range. They are concise and efficient.

This improved version provides a more complete and functional simulation of a virtual co-working space, demonstrating object-oriented principles and simulating interactions between members.  It's also more robust and easier to understand.
👁️ Viewed: 5

Comments