Ruby code refactoring [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
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

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)

How to break inner loop and next for outer loop in ruby? [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 9 years ago.
Improve this question
I dont want to use if statement.
count = 0
10.times do |i|
all = (i..20).collect{ |ii| ii < rand(30) || break }
count+=1 if all # i dont want to use `if` statement
end
p count
How can I do that?
I got something in the similar matter:
count = 0
10.times do |i|
(i..20).collect{ |ii| ii < rand(30) || break } || next
count += 1
end
So it is just boolean algebra. if condition is taken place, when all, i.e. result of collect method, isn't nil, so we need to next keyword worked, when result of collect is nil. Therefore we just set or operator between collect and next, in order to next is occuring when result of collect is nil.
count = (0..9).count { |i| (i..20).all?{ |j| j < rand(30) } }

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}

Simple: Need help refactoring this awkward looking method [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 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) ...

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.

Resources