1

Is there any way for me to use the '@' symbol in a rails method name? e.g.

def @method_name

end

Rails doesn't seem to like it.

I want to do it to adhere to some external conventions (external to rails).

user3711600
  • 853
  • 3
  • 12
  • 27
  • 1
    The simple thing I can say is that `@` symbol has special meaning to many programming languages. Thus, any convention that would enforce its use is (in my own oppinion) a wrong convention. – Ivaylo Slavov Oct 22 '14 at 10:50

2 Answers2

1

It can be done like this:

define_method('@test') do
  'test'
end

this method then has to be called with:

model.send('@test')

I would not recommend it, since it will be ugly and complicated and that is against the philosophy of ruby. But it can be done.

Kind of like this video: https://www.youtube.com/watch?v=eVpVHGiELf8

Albin
  • 2,912
  • 1
  • 21
  • 31
0

What I ended up using:

In my controller:

@job = Job.first
render :json @job.as_json( methods: :@id )

In Job.rb

define_method('@id') do
  return url_for( :controller => 'jobs', :action => 'create', :id => id )
end

Now when I render my json i get:

"@id":"http://localhost:3000/jobs?id=1"

Note: @id is to comply with the syntax of json-ld

user3711600
  • 853
  • 3
  • 12
  • 27