Learning and stuck `OpenStruct` standard library of Ruby

Hi,

I was learning Ruby’s standard library OpenStruct. And doing so,out of
curiosity wrote the below code:

require ‘ostruct’

person = OpenStruct.new
person.name = “John S.”
person.age = 70
person.pension = 300

p person.instance_variables #=> [:@table, :@modifiable]

Now my question is - Where the above 2 instance variables have been
defined? What are their uses.

On the same not I also not able to understand the method #modifiable.
(Class: OpenStruct (Ruby 2.0.0)).

Can any one help me to understand the same?

On 2013-05-24, at 6:44 AM, Love U Ruby [email protected] wrote:

person.pension = 300

p person.instance_variables #=> [:@table, :@modifiable]

Now my question is - Where the above 2 instance variables have been
defined? What are their uses.

On the same not I also not able to understand the method #modifiable.

(Class: OpenStruct (Ruby 2.0.0)).

Can any one help me to understand the same?

You might be able to help yourself by using a tool like pry
http://pryrepl.org to look at the variables and source. For example

ratdog:~ mike$ pry
[1] pry(main)> require ‘ostruct’
=> false
[2] pry(main)>
[3] pry(main)> person = OpenStruct.new
=> #
[4] pry(main)> person.name = “John S.”
=> “John S.”
[5] pry(main)> person.age = 70
=> 70
[6] pry(main)> person.pension = 300
=> 300
[7] pry(main)> person
=> #<OpenStruct name=“John S.”, age=70, pension=300>
[8] pry(main)> cd person
[9] pry(#):1> $ to_s

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

def inspect
str = “#<#{self.class}”

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

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

I have found pry and its add-ons to be very useful for exploring bits of
ruby code.

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

Mike S. wrote in post #1110034:

On 2013-05-24, at 6:44 AM, Love U Ruby [email protected] wrote:

Can any one help me to understand the same?

You might be able to help yourself by using a tool like pry
http://pryrepl.org to look at the variables and source. For example

I have found pry and its add-ons to be very useful for exploring bits of
ruby code.

first time, I saw pry. You mentioned some add-ons you are using.
What are they? How are they useful? any more suggestions please?

I am getting error as below:

kirti@kirti-Aspire-5733Z:~$ gem install pry
Fetching: coderay-1.0.9.gem (100%)
Successfully installed coderay-1.0.9
Fetching: slop-3.4.5.gem (100%)
Successfully installed slop-3.4.5
Fetching: method_source-0.8.1.gem (100%)
Successfully installed method_source-0.8.1
Fetching: pry-0.9.12.2.gem (100%)
Successfully installed pry-0.9.12.2
invalid options: -SNw2
(invalid options are ignored)
Parsing documentation for coderay-1.0.9
Installing ri documentation for coderay-1.0.9
Parsing documentation for slop-3.4.5
Installing ri documentation for slop-3.4.5
Parsing documentation for method_source-0.8.1
Installing ri documentation for method_source-0.8.1
Parsing documentation for pry-0.9.12.2
Installing ri documentation for pry-0.9.12.2
4 gems installed
kirti@kirti-Aspire-5733Z:~$ pry
No command ‘pry’ found, did you mean:
Command ‘pr’ from package ‘coreutils’ (main)
pry: command not found
kirti@kirti-Aspire-5733Z:~$

Thanks

On 2013-05-24, at 5:49 PM, Mike S. [email protected] wrote:

By default I install bundler, pry, and pry-plus as gems for my basic ruby
installs.

GitHub - rking/pry-plus: Pry plus the essential plugins. is a bundle of useful pry add-on gems.

There are many extras for pry, I haven’t yet learned all of pry and pry-plus so
I should not be considered any kind of authority!

Rails Conf 2013 Pry-- The Good Parts! by Conrad Irwin - YouTube is a pry presentation from
railsconf 2013

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

On 2013-05-24, at 3:34 PM, Love U Ruby [email protected] wrote:

first time, I saw pry. You mentioned some add-ons you are using.
What are they? How are they useful? any more suggestions please?

Thanks

By default I install bundler, pry, and pry-plus as gems for my basic
ruby installs.

GitHub - rking/pry-plus: Pry plus the essential plugins. is a bundle of useful pry add-on gems.

There are many extras for pry, I haven’t yet learned all of pry and
pry-plus so I should not be considered any kind of authority!

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.