YAML question: calling a method

hi,

from what I’ve learned from the yaml.rb docs I can initialize an object
in YAML with

!ruby/object:Classname
para1: val1
para2: val2

now how can I call a specific method of this object (in YAML as well)?

benny

Benny wrote:

hi,

from what I’ve learned from the yaml.rb docs I can initialize an object
in YAML with

!ruby/object:Classname
para1: val1
para2: val2

now how can I call a specific method of this object (in YAML as well)?

Not sure what you mean by calling a method in YAML, but you can do this:

require ‘yaml’

S = Struct.new :para1, :para2
str = %{
— !ruby/struct:S
para1: 1
para2: 2
}
s = YAML.load(str)
p s.para1

Benny wrote:

hi,

from what I’ve learned from the yaml.rb docs I can initialize an object
in YAML with

!ruby/object:Classname
para1: val1
para2: val2

now how can I call a specific method of this object (in YAML as well)?

YAML is only a serialisation language… its not a programming language.
It holds data, it can’t perform operations on that data.

···

Joel VanderWerf wrote:

Benny wrote:

hi,

from what I’ve learned from the yaml.rb docs I can initialize an object
in YAML with

!ruby/object:Classname
para1: val1
para2: val2

now how can I call a specific method of this object (in YAML as well)?

Not sure what you mean by calling a method in YAML, but you can do this:

I would like to do something like this

YAML-File

  • !ruby/variable:x = !ruby/object:Some_Cool_Class
    para1: val1
    para2: val2
  • !ruby/variable:y = !ruby/method:x.get_somethings_for_me
  • new data for us
    stuff: !ruby/hash-value: y[‘some_key’]
    other stuff: !ruby/hash-value: y[‘some__other_key’]

parsing Ruby-file

require ‘yaml’
class Some_Cool_Class
def initialize p1, p2
@para1 = p1
@para2 = p2
end

    def get_somethings_for_me
            puts "making something really complicated"
            return {
                    'some_key' => 'some value', 
                    'some__other_key' => 'some other value'
                    }
    end

end

result = YAML.load(str)

benny

Asfand Yar Qazi wrote:

Benny wrote:

hi,

from what I’ve learned from the yaml.rb docs I can initialize an object
in YAML with

!ruby/object:Classname
para1: val1
para2: val2

now how can I call a specific method of this object (in YAML as well)?

YAML is only a serialisation language… its not a programming language.
It holds data, it can’t perform operations on that data.

I only want to place the result of some methods in the YAML-data (as
placeholder) so that it is at the right place when the YAML-file gets
parsed by yaml.rb (see my other posting).

I want to use YAML as template-language for controlling what is done by the
parsing (interpreting) ruby script - not as programming language.

···


http://www.it-is-truth.org/