Simple: Need help refactoring this awkward looking method [closed] - ruby

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How do I clean this up? It reads awkward and is way too long.
def report_total(feed_event, advisor)
count = 0
advisor.activity_feed_events.each do |lead|
if lead == SignupFeedEvent
count += 1
else
if lead.is_a?(feed_event)
if lead.event_date > (Time.now - 7.days)
count += 1
end
end
end
end
return count
end

Maybe you can use:
def report_total(feed_event, advisor)
advisor.activity_feed_events.count do |lead|
lead == SignupFeedEvent ||
(lead.is_a?(feed_event) && lead.event_date > (Time.now - 7.days))
end
end
Does the same, way less code.

Read a good book, for example Refactoring Ruby edition.
Good OO practices recommend not to check for class equality or is_a?, but rather check for the capabilities of the object, for example using respond_to?.
if lead.respond_to?(:event_date) ...

Related

Fibonacci series sequence in ruby [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to write a Fibonacci sequence program in ruby without using the recursion.
can you please help me to write this function.
def fibonacci(n)
final = []
(0..n).each_with_index do |i, _|
next final << i if i.zero? || i == 1
final << final[i - 1] + final[i - 2]
end
final
end
puts fibonacci(10)

Ruby Methods and Method calls? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am following a beginners course on Ruby, trying to define two methods in the editor:
A greeter method that takes a single string parameter, name, and returns a string greeting that person. (Make sure to use return and don't use print or puts.)
A by_three? method that takes a single integer parameter, number, and returns true if that number is evenly divisible by three and false if not.
The error I'm getting is "unexpected end".
def greeter(name)
return "hey" + name + "how are you" + "."
end
greeter(alan)
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
by_three?(12)
You should terminate if statement with end keyword:
def by_three?(number)
if number % 3 == 0
return true
else
return false
end
end
Having said that, this method is written really bad, and it can be much simpler:
def by_three?(number)
number % 3 == 0
end

How can I print out the value of each key in a hash represented by *'s? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my code:
class String
def frequency
chars.each_with_object(Hash.new(0)) do |char, h|
h["#{char.upcase}:"] += 1 if char[/[[:alpha:]]/]
end
end
end
I've tried breaking it down in smaller bit's of code, such as using a .times do loop but I couldn't figure it out
for example:
str = "\*"
h["A:"] = count('a').times do
str
end
Are you trying to do something like:
counts = 'aassssvvvvv'.frequency
counts.each{|key,count| puts key + '*'*count}
# A:**
# S:****
# V:*****
Or if you want to change the key you can do:
counts.each{|key,amount| counts[key] = '*'*amount}

ruby metaprogramming better solution [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How this will work without eval
%w{user_id for_whom_id category_id product_status_id}.each do |f|
code = <<-C
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.#{f} = nil }
end
C
eval code
end
?
This should work:
%w{user_id for_whom_id category_id product_status_id}.each do |f|
it "should fail validation with no #{f}" do
should_fail_on_validation { |p| p.public_send "#{f}=", nil }
end
end
It's best to avoid the eval if possible. And in this case, it isn't necessary.

Ruby code refactoring [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have n-times similar statements
if trigger_data.tt_closed
unless trouble_ticket.changes.key?(:status)
#run = 0
break
end
unless trouble_ticket.changes[:status][1] == "Closed"
#run = 0
break
end
end
if trigger_data.tt_assignee
unless trouble_ticket.changes.key?(:assigned_to)
#run = 0
break
end
unless trouble_ticket.changes[:assigned_to][1] == trigger_data.tt_assignee
#run
break
end
end
How to refactoring that code? Maybe dynamic statement build with pass some hash to input. I'm newbie in metaprogramming. Give me advise please
Well, it may not be exactly what you're looking for, but it shortens code a lot.
Also, I removed those key?() methods, because if key doesn't exist, it just passes nil which in Ruby means the same as false for if statement.
I left it with 2 if statements for the sake of clarity. :)
changes = trouble_ticker.changes
if trigger_data.tt_closed && changes[:status][1] != "Closed"
#run = 0
break
end
if t = trigger_data.tt_assignee && changes[:assigned_to][1] != t)
#run = 0
break
end

Resources