ruby metaprogramming better solution [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 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.

Related

How to group an array in the fastest way? [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 am grouping array items using the below code but it feels slow.
Is there a faster/better way to group?
#tasks_sorted = #tasks.group_by(&:start_date).map do |month, data|
hash = {"date" => month}
data.each {|placement| hash["tasks"] = data}
hash
end
I think below you are looking for :
#tasks_sorted = #tasks.group_by(&:start_date).map do |month, data|
{"date" => month, "tasks" => data }
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}

Assign hash values in a method [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
Say I have this method
def foo(bar1, bar2)
## code
end
How could I implement the code so that, when I call foo('hello', 'world'), the foo method accesses a hash, giving:
{
:bar1 => 'hello',
:bar2 => 'world'
}
Is there a Ruby (Rails?) built in method, or how could I write it?
def foo(bar1, bar2)
names = method(__method__).parameters.map{|e| e[1]}
Hash[names.zip(names.map {|name| eval(name)})]
end
Don't do that. It's ugly and evil. Give me the whole context, you're doing something wrong.
def foo(bar1, bar2)
{bar1: bar1, bar2: bar2}
end
If what Sergio mentions was the intention of the question, then
def foo(bar1, bar2)
Hash[method(__method__).parameters.map{|_, k| [k, eval(k.to_s)]}]
end

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 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