Ruby Symbol

A

AliasX Neo

I have been looking into what exactly a Ruby symbol is and have gotten
some decent info on how they work. I have only one question, how are
they used in functions like in Rails? I see Rails function like:

func("string", :symbol => "string")

How is the combination of the symbol and the string being used inside
the function? Is this some kind of convention?

I don't want to start another war on what symbols are for, I just want a
little clarification on how they are used in the context I provided
above. Thanks.

- Josh
 
H

Hemant Kumar

I have been looking into what exactly a Ruby symbol is and have gotten
some decent info on how they work. I have only one question, how are
they used in functions like in Rails? I see Rails function like:

func("string", :symbol => "string")
How is the combination of the symbol and the string being used inside
the function? Is this some kind of convention?

I don't want to start another war on what symbols are for, I just want a
little clarification on how they are used in the context I provided
above. Thanks.

AFAIK you are passing a string and a Hash as an argument to the above
method.
So implementation would look like:

def func(text,option_hash)
# text contains the string
# option_hash is the hash as usual.
p option_hash[:symbol] #=> "string"
end

Using symbols as Hash keys is convention for sure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top