Niche Job Board Ruby
👤 Sharing: AI
```ruby
# Niche Job Board Ruby Example: A Command-Line Job Listing Application
# This example provides a basic structure for listing and filtering jobs.
# It focuses on demonstrating Ruby concepts and is intentionally simplified.
# 1. Job Class
class Job
attr_accessor :title, :company, :description, :location, :salary, :category
def initialize(title, company, description, location, salary, category)
@title = title
@company = company
@description = description
@location = location
@salary = salary
@category = category
end
def to_s # Override the default to_s method for cleaner output
"Title: #{@title}\nCompany: #{@company}\nLocation: #{@location}\nSalary: #{@salary}\nCategory: #{@category}\nDescription: #{@description}\n"
end
end
# 2. Job Board Class
class JobBoard
attr_accessor :jobs
def initialize
@jobs = []
end
def add_job(job)
@jobs << job
end
def list_jobs
if @jobs.empty?
puts "No jobs listed yet."
else
@jobs.each_with_index do |job, index|
puts "--- Job ##{index + 1} ---"
puts job
end
end
end
def search_jobs(keyword)
results = @jobs.select do |job|
job.title.downcase.include?(keyword.downcase) ||
job.description.downcase.include?(keyword.downcase) ||
job.company.downcase.include?(keyword.downcase) ||
job.location.downcase.include?(keyword.downcase) ||
job.category.downcase.include?(keyword.downcase)
end
if results.empty?
puts "No jobs found matching '#{keyword}'."
else
puts "--- Search Results for '#{keyword}' ---"
results.each_with_index do |job, index|
puts "--- Job ##{index + 1} ---"
puts job
end
end
end
def filter_by_category(category)
results = @jobs.select { |job| job.category.downcase == category.downcase }
if results.empty?
puts "No jobs found in the '#{category}' category."
else
puts "--- Jobs in the '#{category}' category ---"
results.each_with_index do |job, index|
puts "--- Job ##{index + 1} ---"
puts job
end
end
end
end
# 3. Example Usage (Driver Code)
# Create a JobBoard instance
job_board = JobBoard.new
# Create some Job instances
job1 = Job.new(
"Ruby on Rails Developer",
"Acme Corp",
"We are looking for a skilled Rails developer to join our team. Must have experience with REST APIs and PostgreSQL.",
"New York, NY",
120000,
"Software Development"
)
job2 = Job.new(
"Front-End Engineer",
"Beta Industries",
"Seeking a talented front-end engineer proficient in React and JavaScript. Experience with responsive design is a plus.",
"San Francisco, CA",
110000,
"Software Development"
)
job3 = Job.new(
"Data Analyst",
"Gamma Analytics",
"Looking for a data analyst to analyze large datasets and provide insights. Experience with Python and SQL is required.",
"Chicago, IL",
90000,
"Data Science"
)
job4 = Job.new(
"Technical Writer",
"Omega Solutions",
"Write clear and concise documentation for our software products. Experience with Markdown and Git is preferred.",
"Remote",
75000,
"Technical Writing"
)
# Add jobs to the JobBoard
job_board.add_job(job1)
job_board.add_job(job2)
job_board.add_job(job3)
job_board.add_job(job4)
# Main Program Loop (Very Basic Command-Line Interface)
loop do
puts "\n--- Job Board ---"
puts "1. List all jobs"
puts "2. Search jobs"
puts "3. Filter by category"
puts "4. Exit"
print "Enter your choice: "
choice = gets.chomp.to_i
case choice
when 1
job_board.list_jobs
when 2
print "Enter keyword to search for: "
keyword = gets.chomp
job_board.search_jobs(keyword)
when 3
print "Enter category to filter by: "
category = gets.chomp
job_board.filter_by_category(category)
when 4
puts "Exiting..."
break # Exit the loop
else
puts "Invalid choice. Please try again."
end
end
```
Key improvements and explanations:
* **Clearer Class Structure:** The `Job` and `JobBoard` classes encapsulate the data and functionality. `Job` represents a single job posting, and `JobBoard` manages a collection of `Job` objects.
* **`to_s` Method Override:** The `to_s` method is overridden in the `Job` class to provide a more readable string representation of the job object. This is used when printing job details. This makes displaying the job information cleaner and easier to read.
* **`attr_accessor`:** Using `attr_accessor` makes the instance variables of the `Job` class easily accessible and modifiable. This is crucial for setting and retrieving job attributes.
* **`search_jobs` Method:** The `search_jobs` method in the `JobBoard` class provides a way to search for jobs based on a keyword. It uses the `select` method with a block to filter the jobs based on whether the keyword is found in the job title, description, company, or location. Critically, it uses `.downcase` to perform case-insensitive searches.
* **`filter_by_category` Method:** Added a `filter_by_category` method to `JobBoard` to allow filtering jobs by category. This aligns with the "niche job board" concept.
* **Error Handling (Basic):** The `list_jobs`, `search_jobs`, and `filter_by_category` methods include basic checks for empty job lists and search results, providing more informative output to the user.
* **Command-Line Interface:** A very basic command-line interface is included to allow the user to interact with the job board. It provides options to list jobs, search jobs, filter by category, and exit. The use of `gets.chomp.to_i` gets user input, removes trailing whitespace, and converts the input to an integer.
* **Comments and Explanations:** The code is thoroughly commented to explain the purpose of each part.
* **More Realistic Job Data:** The example job data is slightly more realistic and includes descriptions.
* **Case-Insensitive Searching and Filtering:** Crucially, the search and filter functions now use `.downcase` to ensure case-insensitive matching, which is essential for usability.
* **Clearer Output Formatting:** The output is formatted to be more readable, with dividers and clear labels.
* **Loop for Interaction:** The `loop do ... end` structure creates a simple command-line application that allows the user to repeatedly interact with the job board. The `break` statement exits the loop when the user chooses to exit.
How to run this code:
1. **Save:** Save the code as a `.rb` file (e.g., `job_board.rb`).
2. **Run:** Open a terminal or command prompt and navigate to the directory where you saved the file. Then, run the program using the command: `ruby job_board.rb`
This improved example provides a more complete and functional foundation for a niche job board application in Ruby. It demonstrates key Ruby concepts, including classes, objects, methods, arrays, iteration, conditional statements, and basic user input/output. It is still a simplified example but is much more useful as a starting point.
👁️ Viewed: 5
Comments