Invalid Function in Ruby

Invalid function in ruby

In Ruby, you can't surround a required parameter with optional parameters. Using

def request(resource, method='get', strip=true, meta={})
end

will solve the issue.

As a thought experiment, consider the original function

def request(method='get',resource, meta={}, strip=true)
end

If I call that method as request(object), the desired behavior is fairly obvious -- call the method with object as the resource parameter. But what if I call it as request('post', object)? Ruby would need to understand the semantics of method to decide whether 'post' is the method or the resource, and whether object is the resource or the meta. This is beyond the scope of Ruby's parser, so it simply throws an invalid function error.

A couple additional tips:

I would also put the meta argument last, which allows you to pass the hash options in without curly braces, such as:

request(object, 'get', true, foo: 'bar', bing: 'bang')

As Andy Hayden pointed out in the comments, the following function works:

def f(aa, a='get', b, c); end

It's generally good practice to place all your optional parameters at the end of the function to avoid the mental gymnastics required to follow calls to a function like this.

How can I solve undefined method `[]' on Ruby?

When the input is invalid, the call to

@breweries_hash = HTTParty.get("...")

returns not the object you expect (I’d suggest it returns an empty hash.) That makes it impossible to get to details in the following lines. Depending on how are you to handle it, you might decide to e. g. early return from this function, or raise, or do something else.

To approach this, start with debugging the issue, like this:

@breweries_hash = HTTParty.get("...")
puts @breweries_hash.inspect
...

That way you’ll see what gets returned and get the ideas of how to handle it.

If I am right, and what is returned is an empty hash, you might want to early return from this function.

@breweries_hash = HTTParty.get("...")
return if @breweries_hash.empty?
...

Using a question mark in ruby methods

Note that if you comment out the line causing your error (@attacked? = false), you will still get an error relating to the question mark:

NameError: invalid attribute name `attacked?'

The problem is that ? is not a valid character in a variable name. The first error (the SyntaxError that you’re seeing) is caused at parse time and caught immediately. The second error is caused when Ruby tries to evaluate the code and cannot create an instance variable with a name containing an invalid character.

A question mark is a valid character at the end of a method name though (actually it’s possible to have a method with a ? anywhere in its name, but you can’t call such a method directly).

One way to achieve what you want is something like this:

class Player
attr_accessor :name, :health, :attacked
alias :attacked? :attacked

def initialize(name)
@name = name
@health = 100
@attacked = false
end
end

This leaves attacked without the question mark, but adds attacked? as an alias.

Making invalid method names valid

Given your class X, you can generate a symbol that accepts the space character:

s = input.to_sym

and then call, for example, define_singleton_method on your class to define a singleton method:

X.define_singleton_method(s) do
...
end

And then you can use send to call that method on a class X's instance x:

x.send(input.to_sym [, args...])

in `_main': undefined method `run' for nil:NilClass (NoMethodError)

At the point where you call request.run the value for request is nil. This is why you are seeing the error you're given.

This is happening because the line right above it assigns the nil value to the request variable.

You are clearly coming from another language that is not Ruby (some type of C maybe?), by how you've formatted things. It would help for you to get more familiar with Ruby and its idioms. However, best I can tell, you want to do something like this instead:

def _main
request = MySqliteRequest.new
request.from('nba_player_data.csv')
request.select('name')
request.where('birth_state', 'Indiana')
request.run
end
_main()

This assumes you've also defined (and in some cases probably overridden) these methods on your MySqliteRequest Object or Model:

from

select

where

However, please note that the way you're going about this is just completely against how Ruby and Ruby on Rails is designed to work.



Related Topics



Leave a reply



Submit