# Yaml

**URL:** <https://rubytalk.org/t/yaml/44814>\
**Category:** ruby-talk\
**Created:** [27 February 2008 19:24 UTC](https://rubytalk.org/t/yaml/44814 "2008-02-27T19:24:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [27 February 2008 19:24 UTC](https://rubytalk.org/t/yaml/44814/1 "2008-02-27T19:24:58Z")

</div>

Hi,

All I want it to read this YAML as a ruby hash and place into the  
below methods. Could someone help?

test: tiscali  
url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
register:  
&nbsp;&nbsp;first\_name: aidy  
&nbsp;&nbsp;surname: smith  
&nbsp;&nbsp;email: 'testautomated6@googlemail.com'  
&nbsp;&nbsp;password: password  
&nbsp;&nbsp;registration: KJHDF123

tiscali= SmokeTest.new("Tiscali", "[http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)")  
tiscali.register('aidy', 'smith, 'testautomated@googlemail.com',  
'password', 'KJHDF123')

Aidy

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [27 February 2008 19:33 UTC](https://rubytalk.org/t/yaml/44814/2 "2008-02-27T19:33:07Z")

</div>

> All I want it to read this YAML as a ruby hash and place into the  
> below methods. Could someone help?
> 
> test: tiscali  
> url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
> register:  
> &nbsp;&nbsp;first\_name: aidy  
> &nbsp;&nbsp;surname: smith  
> &nbsp;&nbsp;email: 'testautomated6@googlemail.com'  
> &nbsp;&nbsp;password: password  
> &nbsp;&nbsp;registration: KJHDF123
> 
> tiscali= SmokeTest.new("Tiscali", "[http://tiscali3-test.vizumi.com/&quot;\](http://tiscali3-test.vizumi.com/&quot;%5C))  
> tiscali.register('aidy', 'smith, 'testautomated@googlemail.com',  
> 'password', 'KJHDF123')

require 'yaml'

def SmokeTest.create\_from\_yaml\_file(filename)  
&nbsp;&nbsp;data = YAML.load\_file(filename)  
&nbsp;&nbsp;obj = new(data['test'], data['url'])  
&nbsp;&nbsp;obj.register(data['first\_name'], data['surname'], data['email'],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
&nbsp;&nbsp;obj  
end

tiscali = SmokeTest.create\_from\_yaml\_file('tiscali.yml')

> Aidy

--Greg

> **···**
>
> On Thu, Feb 28, 2008 at 04:24:58AM +0900, aidy wrote:

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [27 February 2008 20:44 UTC](https://rubytalk.org/t/yaml/44814/3 "2008-02-27T20:44:55Z")

</div>

Hi Greg

> **···**
>
> On Feb 27, 7:33 pm, Gregory Seidman \<gsslist+r...@anthropohedron.net\> wrote:
> 
> > On Thu, Feb 28, 2008 at 04:24:58AM +0900, aidy wrote:  
> > \> All I want it to read this YAML as a ruby hash and place into the  
> > \> below methods. Could someone help?
> > 
> > \> test: tiscali  
> > \> url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
> > \> register:  
> > \> first\_name: aidy  
> > \> surname: smith  
> > \> email: 'testautomat...@googlemail.com'  
> > \> password: password  
> > \> registration: KJHDF123
> > 
> > \> tiscali= SmokeTest.new("Tiscali", "[http://tiscali3-test.vizumi.com/&quot;\](http://tiscali3-test.vizumi.com/&quot;%5C))  
> > \> tiscali.register('aidy', 'smith, 'testautoma...@googlemail.com',  
> > \> 'password', 'KJHDF123')
> > 
> > require 'yaml'
> > 
> > def SmokeTest.create\_from\_yaml\_file(filename)  
> > data = YAML.load\_file(filename)  
> > obj = new(data['test'], data['url'])  
> > obj.register(data['first\_name'], data['surname'], data['email'],  
> > data['password'], data['registration'])  
> > obj  
> > end
> > 
> > tiscali = SmokeTest.create\_from\_yaml\_file('tiscali.yml')
> > 
> > \> Aidy
> > 
> > --Greg- Hide quoted text -
> 
> Brilliant thanks.
> 
> Aidy

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [28 February 2008 13:04 UTC](https://rubytalk.org/t/yaml/44814/4 "2008-02-28T13:04:57Z")

</div>

Hi Greg,

Thanks for you help but I am having trouble losing the hash value when  
calling the method

one.rb

def SmokeTest.create\_from\_yaml\_file(filename)  
&nbsp;&nbsp;data = YAML.load\_file(filename)  
&nbsp;&nbsp;obj = new(data['test'], data['url'])  
&nbsp;&nbsp;obj.register(data['first\_name'], data['surname'], data['email'],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
end

SmokeTest.create\_from\_yaml\_file('C:\test.yaml')

two.rb

&nbsp;&nbsp;def register\_an\_account(first\_name, last\_name, email, password,  
registration\_code)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enter\_first\_name(first\_name)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;..........  
&nbsp;&nbsp;end

def enter\_first\_name(first\_name)  
&nbsp;&nbsp;$browser.text\_field(:id, /firstname/).set(first\_name)  
&nbsp;&nbsp;$test\_log.test\_results "comment", "first name entered of:  
#{first\_name}"  
end

Cheers

Aidy

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [28 February 2008 13:13 UTC](https://rubytalk.org/t/yaml/44814/5 "2008-02-28T13:13:51Z")

</div>

> Hi Greg,
> 
> Thanks for you help but I am having trouble losing the hash value when  
> calling the method

I don't know what you mean by that.

> one.rb
> 
> def SmokeTest.create\_from\_yaml\_file(filename)  
> &nbsp;&nbsp;data = YAML.load\_file(filename)  
> &nbsp;&nbsp;obj = new(data['test'], data['url'])  
> &nbsp;&nbsp;obj.register(data['first\_name'], data['surname'], data['email'],  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
> end

[...]

This is incorrect. You are missing the last line from the method I  
originally wrote, which is just obj. If you don't have that, the  
create\_from\_yaml method returns whatever obj.register returns, rather than  
the created object itself. This may be the problem you are having.

> Cheers  
> Aidy

--Greg

> **···**
>
> On Thu, Feb 28, 2008 at 10:04:57PM +0900, aidy wrote:

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [28 February 2008 15:30 UTC](https://rubytalk.org/t/yaml/44814/6 "2008-02-28T15:30:38Z")

</div>

Hi,

def SmokeTest.create\_from\_yaml\_file(filename)  
&nbsp;&nbsp;data = YAML.load\_file(filename)  
&nbsp;&nbsp;&nbsp;obj = new(data['test'], data['url'])  
&nbsp;&nbsp;&nbsp;obj.register(data['first\_name'], data['surname'], data['email'],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
&nbsp;&nbsp;obj  
end

SmokeTest.create\_from\_yaml\_file('C:\tiscali.yml')

When I get to obj.register(data['first\_name'], I get a nil object.

My yaml is

test: tiscali  
url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
register:  
&nbsp;&nbsp;first\_name: aidy  
&nbsp;&nbsp;surname: smith  
&nbsp;&nbsp;email: 'testautomated6@googlemail.com'  
&nbsp;&nbsp;password: password  
&nbsp;&nbsp;registration: KJHDF123

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [28 February 2008 16:09 UTC](https://rubytalk.org/t/yaml/44814/7 "2008-02-28T16:09:55Z")

</div>

> test: tiscali  
> url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
> register:  
> first\_name: aidy  
> surname: smith  
> email: 'testautomat...@googlemail.com'  
> password: password  
> registration: KJHDF123

The problem is I am trying to use nested mapping. I have commented out  
register.

Aidy

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [28 February 2008 20:40 UTC](https://rubytalk.org/t/yaml/44814/8 "2008-02-28T20:40:30Z")

</div>

> \> test: tiscali  
> \> url: ?[http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
> \> register:  
> \> ? first\_name: aidy  
> \> ? surname: smith  
> \> ? email: 'testautomat...@googlemail.com'  
> \> ? password: password  
> \> ? registration: KJHDF123  
> \>  
> The problem is I am trying to use nested mapping. I have commented out  
> register.

Ah, yes, that would do it. Sorry, I missed that.

> Aidy

--Greg

> **···**
>
> On Fri, Feb 29, 2008 at 01:09:55AM +0900, aidy wrote:

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [3 March 2008 13:49 UTC](https://rubytalk.org/t/yaml/44814/9 "2008-03-03T13:49:54Z")

</div>

Hi Greg,

> **···**
>
> On 28 Feb, 20:40, Gregory Seidman \<gsslist+r...@anthropohedron.net\> wrote:
> 
> > On Fri, Feb 29, 2008 at 01:09:55AM +0900, aidy wrote:
> 
> There are two things I do not undersand
> 
> 1) why you have added a method to this class:
> 
> def SmokeTest.create\_from\_yaml\_file(filename)
> 
> 2. And how can you get away with instantiating here.
> 
> obj = new(data['test'], data['url'])
> 
> Cheers
> 
> Aidy

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [3 March 2008 14:36 UTC](https://rubytalk.org/t/yaml/44814/10 "2008-03-03T14:36:05Z")

</div>

> Hi Greg,
> 
> There are two things I do not undersand
> 
> 1) why you have added a method to this class:
> 
> def SmokeTest.create\_from\_yaml\_file(filename)
> 
> 2. And how can you get away with instantiating here.
> 
> obj = new(data['test'], data['url'])

Well, it's sort of a constructor built on top of the standard constructor  
(new). It certainly belongs to the class, since it is building an object of  
that class, and because it is a method of that class object, it can call  
new without fully qualifying it (i.e. SmokeTest.new).

Remember that there is nothing terribly special about the new method. I  
believe it does nothing more than call alloc (to get an uninitialized  
object of the class) then call initialize on that object. (I'm sure I will  
be corrected if there is more to it than that.) Likewise, there  
is nothing special about the initialize function. By convention (and  
default implementation), it is called by the class new method, but it's  
just a method like any other.

All I did was write a new constructor for your class, wrapping the default  
constructor.

> Cheers  
> Aidy

--Greg

> **···**
>
> On Mon, Mar 03, 2008 at 10:49:54PM +0900, aidy wrote:
> 
> > On 28 Feb, 20:40, Gregory Seidman \<gsslist+r...@anthropohedron.net\> \> wrote:  
> > \> On Fri, Feb 29, 2008 at 01:09:55AM +0900, aidy wrote:

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [3 March 2008 16:30 UTC](https://rubytalk.org/t/yaml/44814/11 "2008-03-03T16:30:09Z")

</div>

Hi All (greg)

I just can't get the code to iterate through both docs.

def SmokeTest.create\_from\_yaml\_file(filename)  
require 'yaml'  
log = File.open(filename)  
#yp = YAML::load\_documents(log) { |data|  
YAML::each\_document(log) { |data|  
obj = new(data['test'], data['url'])  
obj.register(data['first\_name'], data['surname'], data['email'],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
obj.login\_to\_site(data['email'], data['password'])  
obj.search\_for\_film(data['search\_term'],  
data['expected\_search\_result'])  
obj  
}  
end

SmokeTest.create\_from\_yaml\_file('C:\tiscali.yaml')

test: tiscali  
url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
first\_name: aidy  
surname: lewis  
email: [testautomated6@googlemail.com](mailto:testautomated6@googlemail.com)  
password: password  
registration: KJHDF123  
search\_term: Alien  
expected\_search\_result: Alien 3

test: Vizumi  
url: [http://vizumi3-test.vizumi.com/](http://vizumi3-test.vizumi.com/)  
first\_name: aidy  
surname: lewis  
email: [testautomated6@googlemail.com](mailto:testautomated6@googlemail.com)  
password: password  
registration: KJHDF123  
search\_term: Alien  
expected\_search\_result: Alien 3

Cheers

Aidy

---

<div class="post-metadata">

**Author:** ![Gregory\_Seidman](https://avatars.discourse-cdn.com/v4/letter/g/eb9ed0/32.png) [@Gregory\_Seidman](https://rubytalk.org/u/Gregory_Seidman)\
**Post date:** [3 March 2008 17:00 UTC](https://rubytalk.org/t/yaml/44814/12 "2008-03-03T17:00:40Z")

</div>

> Hi All (greg)
> 
> I just can't get the code to iterate through both docs.

[...]

I am insufficiently familiar with multi-document YAML to help you here.  
Sorry. Perhaps someone else will be able to help.

> Cheers  
> Aidy

--Greg

> **···**
>
> On Tue, Mar 04, 2008 at 01:30:09AM +0900, aidy wrote:

---

<div class="post-metadata">

**Author:** ![\_why](https://avatars.discourse-cdn.com/v4/letter/_/73ab20/32.png) [@\_why](https://rubytalk.org/u/_why)\
**Post date:** [3 March 2008 17:30 UTC](https://rubytalk.org/t/yaml/44814/13 "2008-03-03T17:30:28Z")

</div>

> Hi All (greg)
> 
> I just can't get the code to iterate through both docs.

Hi, question: do you have a document separator between each YAML document?  
It's three dashes.

&nbsp;&nbsp;\>\> docs = \<\<END  
&nbsp;&nbsp;- Document 1

> **···**
>
> On Tue, Mar 04, 2008 at 01:30:09AM +0900, aidy wrote:  
> &nbsp;&nbsp;---  
> &nbsp;&nbsp;- Document 2  
> &nbsp;&nbsp;END  
> &nbsp;&nbsp;=\> "- Document 1\n---\n- Document 2\n"  
> &nbsp;&nbsp;\>\> YAML.each\_document(docs) { |doc| p doc }  
> &nbsp;&nbsp;["Document 1"]  
> &nbsp;&nbsp;["Document 2"]  
> &nbsp;&nbsp;=\> nil
> 
> \_why

---

<div class="post-metadata">

**Author:** ![aidy1](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@aidy1](https://rubytalk.org/u/aidy1)\
**Post date:** [3 March 2008 18:44 UTC](https://rubytalk.org/t/yaml/44814/14 "2008-03-03T18:44:57Z")

</div>

Thanks for the help. Not much documention on YAML. Just feel it is  
right for testing. Doing this and just one iteration running (

def SmokeTest.create\_from\_yaml\_file(filename)  
require 'yaml'  
File.open(filename) do |yf|  
YAML.each\_document(yf) do |data|  
obj = new(data['test'], data['url'])  
obj.register(data['first\_name'], data['surname'], data['email'],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data['password'], data['registration'])  
obj.login\_to\_site(data['email'], data['password'])  
obj.search\_for\_film(data['search\_term'],  
data['expected\_search\_result'])  
obj  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

SmokeTest.create\_from\_yaml\_file('C:\tiscali.yaml')

(one file)  
test: tiscali  
url: [http://tiscali3-test.vizumi.com/](http://tiscali3-test.vizumi.com/)  
first\_name: aidy  
surname: smith  
email: 'testautomated6@googlemail.com'  
password: password  
registration: KJHDF123

> **···**
>
> ---
> 
> test: vizumi  
> url: [vizumi3-test.vizumi.com](http://vizumi3-test.vizumi.com)  
> first\_name: aidy  
> surname: smith222  
> email: 'testautomated6@googlemail.com'  
> password: password  
> registration: KJHDF123
