[#407882] Ruby extremely slow compared to PHP — Mick Jagger <lists@...>

Hello there, how are you? Hope you are fine. I am a PHP programmer

17 messages 2013/06/02

[#407908] TCPServer/Socket and Marshal problem — Panagiotis Atmatzidis <atma@...>

Hello,

18 messages 2013/06/03

[#407946] Is rubyquiz.com dead? — Alphonse 23 <lists@...>

Thread title says everything.

18 messages 2013/06/04

[#408012] Need help understanding recursion. — pedro oliva <lists@...>

Ive been reading Chris Pine's book 'Learn to Program' and its been going

11 messages 2013/06/06

[#408129] Getting Started With Development — Chamila Wijayarathna <cdwijayarathna@...>

I'm new to Ruby Development. I downloaded source from Github, but couldn't

24 messages 2013/06/11
[#408131] Re: Getting Started With Development — Per-erik Martin <lists@...> 2013/06/11

Ruby is often installed on linux, or can be easily installed with the

[#408146] Re: Getting Started With Development — "Chamila W." <lists@...> 2013/06/11

Per-erik Martin wrote in post #1112021:

[#408149] Re: Getting Started With Development — "Carlo E. Prelz" <fluido@...> 2013/06/11

Subject: Re: Getting Started With Development

[#408198] NokoGiri XML Parser — "Devender P." <lists@...>

Hi,

11 messages 2013/06/13

[#408201] trying to load a .rb file in irb — "Eric D." <lists@...>

I am trying to load a ruby program into irb and it will not load.

12 messages 2013/06/13

[#408205] Can I use Sinatra to render dynamic pages? — Ruby Student <ruby.student@...>

Hell Team,

18 messages 2013/06/13
[#408219] Re: Can I use Sinatra to render dynamic pages? — Nicholas Van Weerdenburg <vanweerd@...> 2013/06/14

You should be able to do this without JavaScript by using streaming.

[#408228] Re: Can I use Sinatra to render dynamic pages? — Ruby Student <ruby.student@...> 2013/06/14

Well, I got some good suggestions from everyone here. I thank you all for

[#408275] Compare and sort one array according to another. — masta Blasta <lists@...>

I have two arrays of objects that look something like this:

14 messages 2013/06/17

[#408276] Comparing objects — "Thom T." <lists@...>

How do I compare two objects in Ruby, considering only attributes

15 messages 2013/06/17

[#408307] getting the most out of Ruby — robin wood <lists@...>

I write a lot of scripts in Ruby, most are small simple things but some

13 messages 2013/06/18

[#408309] Creating ruby script exe — Rochit Sen <lists@...>

Hi All,

17 messages 2013/06/18

[#408357] Beginners problem with database and datamapper — cristian cristian <lists@...>

Hi all!

28 messages 2013/06/20

[#408437] How do I input a variable floating point number into Ruby Programs — "Michael P F." <lists@...>

I want to evaluate the following interactively:

10 messages 2013/06/23

[#408518] #!/usr/bin/env: No such file or directory — Todd Sterben <lists@...>

I am new to both linux and ruby. I am using Ubuntu and Ruby 1.9

17 messages 2013/06/27

[#408528] Designing a Cabinet class — Mike Vezzani <lists@...>

Hello all,

12 messages 2013/06/27

[#408561] Find elment in array of hashes — Rodrigo Lueneberg <lists@...>

array = {:id=>1, :price =>0.25} # index[0]

23 messages 2013/06/28

Re: Why the `#new_ostruct_member` not being able to add the `:email` ?

From: Mike Stok <mike@...>
Date: 2013-06-20 12:24:50 UTC
List: ruby-talk #408355
How do you know that it isn't able to add :email?

ratdog:tmp mike$ pry
[1] pry(main)> require 'ostruct'
=3D> false
[2] pry(main)> contact =3D OpenStruct.new(first_name: "John", last_name: =
"Smith", phone: "XXXXXXX")
=3D> #<OpenStruct first_name=3D"John", last_name=3D"Smith", =
phone=3D"XXXXXXX">
[3] pry(main)> ls contact
OpenStruct#methods:
  =3D=3D  []=3D           each_pair  hash     marshal_dump  =
method_missing  to_s
  []  delete_field  eql?       inspect  marshal_load  to_h
self.methods: first_name  first_name=3D  last_name  last_name=3D  phone  =
phone=3D
instance variables: @table

We see that contact's methods unique to itself are the expected =
setters/getters for first_name, last_name, and phone. Let's send the =
new_ostruct_member message:

[4] pry(main)> contact.send(:new_ostruct_member ,:email)
=3D> :email
[5] pry(main)> ls contact
OpenStruct#methods:
  =3D=3D  []=3D           each_pair  hash     marshal_dump  =
method_missing  to_s
  []  delete_field  eql?       inspect  marshal_load  to_h
self.methods:
  email  email=3D  first_name  first_name=3D  last_name  last_name=3D  =
phone  phone=3D
instance variables: @table

Now we wee that contact's methods include email and email=3D so it looks =
like new_ostruct_member did its metaprogramming (see =
http://www.ruby-doc.org/stdlib-2.0/libdoc/ostruct/rdoc/OpenStruct.html#met=
hod-i-new_ostruct_member for the docs).

Maybe it was the lack of an email=3Dnil or something in the output of p =
which made you suspect nothing had happened? If so we can look at the =
implementation of inspect to see how it works:

[6] pry(main)> $ contact.inspect

From: /Users/mike/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/ostruct.rb @ =
line 23
Owner: OpenStruct
Visibility: public
Number of lines: 21

def inspect
  str =3D "#<#{self.class}"

  ids =3D (Thread.current[InspectKey] ||=3D [])
  if ids.include?(object_id)
    return str << ' ...>'
  end

  ids << object_id
  begin
    first =3D true
    for k,v in @table
      str << "," unless first
      first =3D false
      str << " #{k}=3D#{v.inspect}"
    end
    return str << '>'
  ensure
    ids.pop
  end
end

So it looks like email will not show up until it has been added to =
@table - looking at email=3D we can see how it works, going through =
modifiable:

[7] pry(main)> $ contact.email=3D

From: /Users/mike/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/ostruct.rb @ =
line 170:
Owner: #<Class:#<OpenStruct:0x007fbe0e567470>>
Visibility: public
Number of lines: 1

define_singleton_method("#{name}=3D") { |x| modifiable[name] =3D x }
[8] pry(main)> $ contact.modifiable

From: /Users/mike/.rbenv/versions/2.0.0-p195/lib/ruby/2.0.0/ostruct.rb @ =
line 151:
Owner: OpenStruct
Visibility: protected
Number of lines: 8

def modifiable
  begin
    @modifiable =3D true
  rescue
    raise TypeError, "can't modify frozen #{self.class}", caller(3)
  end
  @table
end=20

So we can cheat and manually add an "illegal" (i.e. no getter or setter =
defined) entry to contact's @table to see if affects the output of =
inspect:

[9] pry(main)> cd contact
[10] pry(#<OpenStruct>):1> @table
=3D> {:first_name=3D>"John", :last_name=3D>"Smith", :phone=3D>"XXXXXXX"}
[11] pry(#<OpenStruct>):1> @table[:foo] =3D 'bar'
=3D> "bar"
[12] pry(#<OpenStruct>):1> cd ..
[13] pry(main)> p contact
#<OpenStruct first_name=3D"John", last_name=3D"Smith", phone=3D"XXXXXXX", =
foo=3D"bar">
=3D> #<OpenStruct first_name=3D"John", last_name=3D"Smith", =
phone=3D"XXXXXXX", foo=3D"bar">

So we can see that for an attribute to show up in the output of inspect =
it needs to have been put in the OpenStruct instance's @table.

I'll leave it to you to have a look at contact's method_missing to see =
how OpenStruct attributes might be set and retrieved so you can see the =
context in which new_ostruct_member is called.

pry is a great tool.

Hope this helps,

Mike

On 2013-06-20, at 6:09 AM, Love U Ruby <lists@ruby-forum.com> wrote:

> require 'ostruct'
>=20
> contact =3D OpenStruct.new(first_name: "John", last_name: "Smith", =
phone:
> "XXXXXXX")
>=20
> contact.send(:new_ostruct_member ,:email) #=3D "john.doe@mymail.com"
>=20
> p contact #=3D> #<OpenStruct first_name=3D"John", last_name=3D"Smith",
> phone=3D"XXXXXXX">
>=20
> Why the `#new_ostruct_member` not being able to add the `:email` ?
>=20
> --=20
> Posted via http://www.ruby-forum.com/.

--=20

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.







In This Thread