-2

I have this string: 'industry.in' and I want to convert it to :industry.in. When I do 'industry.in'.to_sym, the result is: :"industry.in".

Anyone know how to make it into: :industry.in instead?

I'm doing this so I could make a criteria query in Mongoid for an array field:

criteria = 'industry.in'.to_sym    
Company.where(criteria => ['Information Technology'])
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
Gjaldon
  • 5,534
  • 24
  • 32

3 Answers3

5

:"industry.in" is actually the industry.in symbol represented in a way that's copy/pastable.

If you type :industry.in directly you will get a "No method" error as Ruby will parse it as:

call #in method on :industry symbol

So, 'industry.in'.to_sym is actually doing what you need.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
keymone
  • 8,006
  • 1
  • 28
  • 33
  • My guess is that he's trying to build a query criteria. Something like `User.where(:age.in => [1, 2, 3])`. – Sergio Tulentsev Feb 13 '13 at 09:13
  • and he can do that using `:"industry.in"` notation as it is exactly that - a notation. a way to represent an object so that parser doesn't freak out. – keymone Feb 13 '13 at 09:17
  • unless we're talking about some crazy monkey-patching of symbol objects in which case some library is not loaded.. – keymone Feb 13 '13 at 09:18
  • "is actually doing what you need" - I'm not sure whether you (or I) know what he needs :) – Sergio Tulentsev Feb 13 '13 at 09:36
  • agree. let's wait for author to clarify, i only explained the most straightforward scenario. – keymone Feb 13 '13 at 09:37
  • @SergioTulentsev, you were right that I'm building a query criteria using Mongoid @keymone, using `:"industry.in"` doesn't work when doing the query. Had to manually assign :industry.in to a variable. Would be great though if there was some way to convert the `"industry.in"` string to Symbol. Any ideas? Or is it even possible? – Gjaldon Feb 13 '13 at 14:22
  • @Gjaldon: sure it's possible. See my answer :) – Sergio Tulentsev Feb 13 '13 at 14:31
1

It's quite simple, actually. But first you have to understand what you're trying to do.

:industry.in

Here :industry is a Symbol, and in is a method call on that symbol. So, split the string into two parts, cast first part to symbol and use second part to call a method dynamically.

require 'mongoid'

s = 'industry.in'
parts = s.split('.') # => ["industry", "in"]
parts[0].to_sym.send(parts[1]) # => #<Origin::Key:0x007fa872ec0550 @name=:industry, @strategy=:__intersect__, @operator="$in", @expanded=nil, @block=nil>


# just the same as literal
:industry.in # => #<Origin::Key:0x007fa872ebf970 @name=:industry, @strategy=:__intersect__, @operator="$in", @expanded=nil, @block=nil>
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • This is exactly the answer I was looking for. Thanks! Will update the question with your answer. – Gjaldon Feb 13 '13 at 14:35
  • 2
    Don't copy an answer into your question on Stack Overflow. Anyone viewing can easily see the answer you've selected. – the Tin Man Feb 13 '13 at 15:10
-4

Rails method - to_sym

These are same things.

'industry.in'.to_sym becomes :industry.in

Also, Please see this for more about to_sym.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • 1
    I don't think so. It becomes `:"industry.in"`, which is completely different thing – Sergio Tulentsev Feb 13 '13 at 09:34
  • You haven't read the SO link that I posted in the reply I suppose. Moreover, it is tested by me. Be sure to negate first. – sjain Feb 13 '13 at 09:40
  • I did test as well. Have *you* read that link? It doesn't cover this exact matter. – Sergio Tulentsev Feb 13 '13 at 09:42
  • As posted there- "It's not specific to Rails, it looks like in some versions of Ruby, a symbol could be converted to and from a Fixnum as well." The matter is not exactly the same but it points to your first comment to be wrong in its sense. But again, it says its ruby specific. So I raise my hands. – sjain Feb 13 '13 at 09:49
  • 2
    Do you seriously not see the difference between `:industry.in` and `:"industry.in"`? – Sergio Tulentsev Feb 13 '13 at 09:56
  • The difference is there but that's not the question to be asked. In my answer and in the link that I have posted, there is nothing like :"industry.in". I haven't commented about it too. – sjain Feb 13 '13 at 09:59
  • 2
    Well, read my first comment again. Your answer contains false information. – Sergio Tulentsev Feb 13 '13 at 10:00
  • If my answer is false than the answer that got 13 upvotes is also false. See the heading of my post it is about `to_sym` to be more specific. If it is not exactly the answer, then also it is atleast helpful in regard of the question being asked. – sjain Feb 13 '13 at 10:01
  • @SaurabhJain, your answer is false. Converting the string `'industry.in'` to symbol returns `:"industry.in"` and not `:industry.in`. Try it out in IRB. – Gjaldon Feb 13 '13 at 14:26