Missing Method Errors

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.

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

Ari Brown wrote:

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?

···

--
Alex

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.

···

On Apr 20, 2007, at 10:20 PM, james.d.masters@gmail.com wrote:

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

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

···

On 4/21/07, Ari Brown <ari@aribrown.com> wrote:

On Apr 20, 2007, at 10:20 PM, james.d.masters@gmail.com wrote:
> 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