user3711600
user3711600

Reputation: 873

Use @ symbol in a rails method name

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).

Upvotes: 1

Views: 134

Answers (2)

user3711600
user3711600

Reputation: 873

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

Upvotes: 0

Albin
Albin

Reputation: 3012

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

Upvotes: 1

Related Questions