Missing Method Errors

A

Ari Brown

Hi all,
This is my first post here, so don't dish out the loser points to me
so heartily.

I have only recently gotten into Ruby, and I was writing my own
methods that I might use in a couple future programs (they were YAML
things, like open and closing a file, and reading a file). I saved it
as yaml.rb . However, when I require it ( require 'yaml.rb') and then
try to call a method I had defined in yaml.rb, I get an error saying
that the method had not been defined yet:

test.rb:8: undefined method `yaml_save' for main:Object (NoMethodError)

yaml_save was the method I had tried to use.
Any thoughts? Suggestions? Flames?

Part II
The missing command

Today I was in the car and was coding on my dad's laptop (Windows
XP), and I was writing a shuffling algorithm. I had used the .pop
command in it once, and it worked. But then all of a sudden Ruby was
saying that there was no command 'pop'. The syntax I had used it in
was like so:

b = some_array.pop

Once again, any thoughts? Suggestions? Flames?

--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
 
J

james.d.masters

test.rb:8: undefined method `yaml_save' for main:Object (NoMethodError)
yaml_save was the method I had tried to use.
Any thoughts? Suggestions? Flames?

The code that cause this issue would be nice... but it looks like you
are calling it from an Object something like this:

a = Object.new
a.yaml_save

Anyway, this message is saying that the method that you are trying to
call could not be found (i.e. it's undefined). Try this:

a = Object.new
a.asdf

Same error...
 
A

Alex Young

Ari said:
Hi all,
This is my first post here, so don't dish out the loser points to me
so heartily.

I have only recently gotten into Ruby, and I was writing my own methods
that I might use in a couple future programs (they were YAML things,
like open and closing a file, and reading a file). I saved it as yaml.rb
.
That's unlucky - there's a yaml.rb in the standard library which require
loads in preference to yours. Try renaming your yaml.rb, or moving it
into a folder so that you can call "require 'ari/yaml'", or some
equivalent. As an aside, it's usual (and, under some circumstances,
required) to leave off the file extension with require.
Part II
The missing command

Today I was in the car and was coding on my dad's laptop (Windows XP),
and I was writing a shuffling algorithm. I had used the .pop command in
it once, and it worked. But then all of a sudden Ruby was saying that
there was no command 'pop'. The syntax I had used it in was like so:

b = some_array.pop

Sounds like some_array isn't actually an array. Care to post the code
that's causing the error?
 
A

Ari Brown

The code that cause this issue would be nice... but it looks like you
are calling it from an Object something like this:

a = Object.new
a.yaml_save

The code I'm using is this:

require 'yaml.rb'

filename = 'fun.txt'
test_array = ['hey',
'look',
'zoo']

yaml_save test_array, filename

read_array = yaml_load filename

puts(read_array == test_array)

The yaml.rb code is:

require 'yaml'

def yaml_save object, filename
File.open filename, 'w' do |f|
f.write(object.to_yaml)
end
end

def yaml_load filename
yaml_string = File.read filename

YAML::load yaml_string
end



~ Ari
English is like a pseudo-random number generator - there are a
bajillion rules to it, but nobody cares.
 
H

hemant

The code that cause this issue would be nice... but it looks like you
are calling it from an Object something like this:

a = Object.new
a.yaml_save

The code I'm using is this:

require 'yaml.rb'

filename = 'fun.txt'
test_array = ['hey',
'look',
'zoo']

yaml_save test_array, filename

read_array = yaml_load filename

puts(read_array == test_array)

The yaml.rb code is:

require 'yaml'

def yaml_save object, filename
File.open filename, 'w' do |f|
f.write(object.to_yaml)
end
end

def yaml_load filename
yaml_string = File.read filename

YAML::load yaml_string
end


You should really use, some other name for your library because as
pointed Alex, its in conflict with ruby standard library.
 

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,240
Messages
2,571,211
Members
47,849
Latest member
RoseannKoz

Latest Threads

Top