what's the difference between -> and => in rails [closed] - ruby-on-rails

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
today I follow the rails guide and run a demo which include a scope as below:
scope :me, =>(keyword){where("title = ?",keyword)}
but it not work,so I change to :
scope :me, ->(keyword){where("title = ?",keyword)}
now it works,so I want to know the the difference between -> and => in rails
but I didn't find the result,so please tell me,thank you.

=> separates the keys from the values in a hashmap literal
-> - new lambda (syntactic sugar)
Examples :
h = { "foo" => "bar" }
l = ->{ "hello" }
l.call # => "hello"

The first is a syntax error. Wherever you read that, it's completely wrong.
The second is commonly known as 'stabby lambda syntax' - its a shortcut for writing:
lambda { |keyword| where('title = ?', keyword) }
More about lambdas in Ruby: http://rubymonk.com/learning/books/1-ruby-primer/chapters/34-lambdas-and-blocks-in-ruby/lessons/77-lambdas-in-ruby

Related

Lua: Getting passed args when called function [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 2 years ago.
Improve this question
I want to get the parameters that passed to a specific function.
for example:
load("return 2+1")()
wanted output:
return 2+1
I have no idea after reading debug library :(
If I correctly understand what you want, override the load function prior to calling it:
local global_load = load
local function load (...)
print (...) -- or use whatever debug tool to see the arguments.
return global_load (...)
end
You can redefine any function this way:
local function verbose (func)
return function (...)
print (...) -- or use whatever debug tool to see the arguments.
return func (...)
end
end
local load = verbose (load)
print (load 'return 2 + 1' ())

Heroku infinite loop [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 6 years ago.
Improve this question
def incoming
sender = params[:From]
body = params[:Body]
#subscription = Subscription.all
twiml = Twilio::TwiML::Response.new do |r|
#subscription.each do |subs|
if (("+1"+(subs.customer.phone_number.to_s)) == sender) && (body.downcase == "unfollow")
r.Message "You are unsubscribed."
subs.destroy
elsif ("+1"+(subs.customer.phone_number.to_s)) == sender)
r.Message "I don't know that command."
else
end
end
end
render xml: twiml.text
end
when I try to deploy the code above to heroku, heroku app crashes.
It works well without this part of the code.
I looked at the heroku log and looks like its making a infinite loop in this method.
how can I make this into non-infinite loop?
problem solved. it was that extra parenthesis.

Json and Ruby creating a string [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
I have a method that I can to push to a JSON call. The function looks like:
def my_function(name, text , id_person, id_company)
end
What I want is to get:
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
Is there any easy way to do this?
The question is a bit unclear, but let me try with few answer possibilities
First of all this is not a JSON format
{"my_function":{"name":"names_value","text":"text_value ","id_person":"id_person_value","id_company":"id_company_value"}
There should not be double quote (") before semicolon (:)
obj = {
my_function:
{
name: "names_value",
text: "text_value",
id_person: "id_person_value",
id_company: "id_company_value"
}
}
If you want to get each of the data in that "JSON" format, you could do this
name = obj['my_function']['name'] #name_value
text = obj['my_function']['text'] #text_value
and so on...
But if you want to make a string data into JSON format, you could do this
def my_function(name_value, text_value, id_person_value, id_company_value)
{
my_function:
{
name: name_value,
text: text_value,
id_person: id_person_value,
id_company: id_company_value
}
}.to_json
end
I hope it's answering your question...

How to get hours from array of datetime attributes? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
=> [#<WelcomeCall id: 16, call_at: "2013-11-06 12:00:00">,
#<WelcomeCall id: 17, call_at: "2013-11-06 13:00:00">,
#<WelcomeCall id: 18, call_at: "2013-11-06 17:00:00">,
#<WelcomeCall id: 19, call_at: "2013-11-06 14:00:00">]
I would like to get an array of hours form all these WelcomeCall objects.
=> [12, 13, 17, 14]
WelcomeCall is rails model.
I used for that:
def self.scheduled_hours date
by_date(date).map do |wc|
wc.call_at.hour
end
end
but maybe there is better way?
You want to use map.
Try something like this:
new_array = old_array.map{ |welcome_call| welcome_call.call_at.strftime('%H').to_i }
Assuming that your welcome_call.call_at is a Date. If it's not a Date object, then try:
new_array = old_array.map do |welcome_call|
date = Date.parse(welcome_call.call_at)
date.strftime('%H').to_i
end
You could iterate over the array and parse the call_at datetime object using strftime('%H').to_i.
welcome_calls.map {|w| w.hour}
Parsing the Date to a String and then reparsing the String to a Fixnum is not as fast I think.
I just tried something and it worked, i'm not sure if it's the right way.
var = Model.all
t = var.collect(&:call_at)
hours_arr = []
t.each do |value|
hours_arr << value.time.hour
end

How can I put conditionals in my controller? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm having problems when I'm trying to put a condition in params in my controller. This code is inside my controller:
if params[:example] == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
Is this code correct? How can I put a conditional in params controller?
You code looks okay. Unfortunately you do not say what problem you have. The only thing I see that may fail is the condition. If you pass some value into your params they are not typecasted. Therefore I guess you should use to_i in your condition:
if params[:example].to_i == 1
...
Try with
#table = Model.where((params[:example] == 1 ? 'column_table= ?' : 'column_table2 = ?'), params[:example]) unless params[:example].blank?
Here is code. You have save params[:example] in one variable.and use your coditions.
if params[:example].present?
#example = params[:example]
if #example == 1
#table = Model.find(:all,:conditions=>['column_table= ?',params[:example] ] )
else
#table = Model.find(:all,:conditions=>['column_table2= ?',params[:example] ] )
end
You can also send the column name along with its value and avoid ifs
#table = Model.find(:all,:conditions=>["#{params[:col]} = ?", params[:example] ] )

Resources