How to use local value globally? Lua [closed] - lua

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
Well, i was trying this for 3 days, nothing found on web.
What i am doing is this:
stringValue = ""
function()
stringValue = "Test"
end
And then i am trying to get the value in another function by this:
function()
print(stringValue);
end
And it returns nothing, sometimes NIL sometimes blank.
I was thinking if someone could help me? I'm sorry if its already asked ... If it was, kindly give me link i am new here.

This should work, you have to invoke the function
stringValue = ""
function a()
stringValue = "Test"
end
function b()
print(stringValue);
end
a()
b()

Global variables are bad (in many cases)!
They increase coupling and ruin any design if used carelessly.
Just don't do it.
Apart from that, you haven't called your first function, if you did, you'd get the needed value.

Related

Random name ( RUBY ) PLEASE [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 1 year ago.
Improve this question
Have the function Cus_free_makers_eg1heChallenge(str) take the name parameter being passed to return "Hello Cristina!" if the name parameter is equal to "Cristina", return "Hello Kay!" if the name parameter is equal to "Kay" or "Hello there!" if the name parameter is equal to any other.
def Cus_free_makers_eg1heChallenge(str)
return str
end # keep this function call here
puts Cus_free_makers_eg1heChallenge(STDIN.gets)
Welcome to Stack Overflow! This is a bit off-topic, but first off, you should probably keep the method name shorter and simpler - probably not more than one or two words. Otherwise, when you call the method, it's a mouthful to type. The name should also describe what the method does, so the code is more readable.
Here is how I would write the method:
def hello(name)
if name == "Cristina" or name == "Kay"
return "Hello #{name}!"
end
"Hello there!"
end

Resolve Fixnum error [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 6 years ago.
Improve this question
I have a parameter (params[:authorization]) that comes from the URL, as you can see:
authorization_selected = params[:authorization]
new_parcel = params[:new_parcel].to_i
puts authorization_selected.class (in the console show type String)
puts new_parcel.class (in the console show type Fixnum)
In my controller, have:
#portability = Portability.new
#portability.employee_id = authorization_selected.employee_id
However this returns an error:
undefined method `employee_id' for 3:Fixnum
I need that both was integer. How do it?
You are calling the employee_id method on authorization_selectedwhich is a String and does not provide this method.
Obviously this does not work. You probably want to do
#portability = Portability.new
#portability.employee_id = authorization_selected
assuming that params[:employee] contains the employee_id and Portability is an ActiveModel or an ActiveRecord.
Perhaps you can change your form that the value can be assigned through the initializer?

Use variable in parameter ruby on rails? [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 7 years ago.
Improve this question
I want to check if param key exists with a variable name and if it exists I want to use value something like params[filenamestring[-1]].
filenamestring is any array generate with split
generally we use params like params[:key] but here i have array and want to use params value with array last element like params[filenamestring[-1]]
You are looking for this:
if params.key?(filenamestring[-1])
This will check if the key exists within the params.
Edit: Something like this would add the param to an array:
my_array << params[filenamestring[-1]] if params.key?(filenamestring[-1])
Or to add it to a string or integer:
my_variable + params[filenamestring[-1]] if params.key?(filenamestring[-1])
If you are doing something else, let me know and I'll update my answer again.

using an external .rb in a controller [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I currently have a separate .rb file that contains smth like this:
if lang == 'fr'
#a = 'AAAAAAA'
#b = 'BBBBBBB'
#c = 'CCCCCCC'
else
#a = 'sadadddsad'
#b = 'dsafdsfdasfdsa'
#c = 'dsadasfdsfsfd'
end
, only with a lot more strings.
Being a large library of strings, and wanting to keep this in one place as these will be used in multiple controllers and functions within, what is a good method to call these files from inside a function in controllers?
Looks like you want to use Internationalization in your app. You could follow this link http://guides.rubyonrails.org/i18n.html to know more and its implementation.

Ruby: more readable way to write nested if conditions? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a more readable way in Ruby (or using Rails model helpers) to write the following:
def get_question
if self.is_question?
self.trackable
elsif self.is_answer?
self.trackable.question
elsif self.is_comment?
if self.trackable.is_question?
self.trackable.commentable
elsif self.trackable.is_answer?
self.trackable.commentable.question
end
end
end
There must be a more "Ruby way" of writing this so it's easier to read for other developers.
I tend to write that sort of thing like this:
def get_question
return self.trackable if self.is_question?
return self.trackable.question if self.is_answer?
return nil if !self.is_comment?
return self.trackable.commentable if self.trackable.is_question?
return self.trackable.commentable.question if self.trackable.is_answer?
return nil
end
that's pretty mu-idiomatic but I don't know if it qualifies as Ruby-idiomatic. Yes, there's an unnecessary return at the end but I like the symmetry and how it makes everything visually line up.
In real life, I'd probably want replace all that logic with a question method on the self.trackable. Then each thing could implement question (or to_question, get_question, or whatever name made the most sense in the broader context):
# Inside questions...
def question
self
end
# Inside answers...
# Nothing special needed, we've already got one.
and so on for the other possible possible self.trackable things. That would leave your get_question looking like this:
def get_question
self.trackable.respond_to?(:question) ? self.trackable.question : nil
end
or you could do away with get_question completely if you knew that self.trackable would also respond to question.

Resources