To convert variable name to string or symbol - ruby-on-rails

it's possible in rails convert variable name to string?
Example:
stack_overflow = "Fantastic!".
by stack_overflow.to_sym I'll get :Fantastic!.
Instead I would => :stack_overflow
and, if it were possibile, this would be bad programming cause unsafe code?
Thanks :-)

You'd need to use eval(), although I don't know the specific way you'd achieve what you want:
new_var = eval(stack_overflow.to_sym)

Related

Compare variable to text disregarding caps [duplicate]

Here is the thing. I am trying to convert a string in lowercase in Lua, but it's not working. I have done this
String = String:lower()
but it doesn't like it. I am sure that is the way to do it, I've seen it done before. A few sites suggest it might be a problem caused by a wrong version of the interpreter.
Any ideas?
You're right, this is one of the ways to do it. It would only not work and throw errors if your "String" variable is not a string.
Personally, i usually prefer to use something like..
myString = string.lower(myString)
But its really the same as doing
myString = myString:lower()
assuming that myString is actually a string, however.
The "long" version has one advantage, it actually works if myString is a number, while the second one errors in that case.

How can I take this text and store it in a variable?

I'm not sure if I can accomplish this with Lua, but I want to take the data from the following URL:
http://www.roblox.com/Trade/InventoryHandler.ashx?token=%22&filter=0&userid=1210210&page=1&itemsPerPage=14&_=
and store it in a variable so I can manipulate it.
Is there any way I can do this?
To save a string as a variable it is as simple as
foo = "contents of string"
To manipulate the string, you would use the string library
If you want the text at the URL instead of in the url, see the link to the previously asked question I added in the comments

How to add prefix and remove double quotes from the value in ruby on rails?

I have a value called "FooBar". I want to replace this text with the quotes to Enr::Rds::FooBar without quotes.
Update:
For example, #question.answer_model gives the value "FooBar" (with the quotes)
I am a newbie and somebody please refer me how would i start to know about regex? What is the best way to practice in online?
Since you want to: a) drop the quotes and b) prepend FooBar with Enr::Rds::, I would suggest you preform exactly what is intended, literally:
'"FooBar"'.delete('"').gsub(/\A/, "Enr::Rds::")
# => "Enr::Rds::FooBar"
I think you are trying to convert a string to a constant. try the following
"Enr::Rds::#{#question.answer_model.gsub('"', '')}".constantize.find(...)

passing variable to a string in a Rails controller

I need to pass a variable into a string like this.
HTTParty.get(https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false)
This works fine as a string, but I cant seem to get the syntax correct to make the parameters variables. Can someone please help me with the syntax.
It's not clear what issue you're having (or if the missing quotes are just a typo). Normal string interpolation should do what you want:
bar = "123.45"
plugh = "xyz"
HTTPart.get("blahblah?foo=#{bar}&baz=#{plugh}")
You'll want to url-encode the values as well, most likely, unless they're under control.

Find all URL's in a string in rails 3

I'm building an app in Rails 3 and I need a method to extract all urls from a string and store them in a hash or something. I know I need to use regular expressions but I don't know where exactly to begin with them.
Also, I know about auto_link, but it doesn't quite do what I'm trying to achieve. I just simply need a hash of all the url's from a string.
Thanks!
From http://www.regular-expressions.info/ruby.html
"To collect all regex matches in a string into an array, pass the regexp object to the string's scan() method, e.g.: myarray = mystring.scan(/regex/)."
So you probably need strings that start with "http". So check the docs for that :)
I don't program in Ruby and I'm not very good with regex but maybe this will help you out:
http://www.ozzu.com/programming-forum/url-regex-t104809.html

Resources