YAML question: calling a method

B

Benny

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
 
J

Joel VanderWerf

Benny said:
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
 
A

Asfand Yar Qazi

Benny said:
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.
 
B

Benny

Joel said:
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
 
B

Benny

Asfand said:
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.
 

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

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,371
Latest member
Brkaa

Latest Threads

Top