How to output an instance's type

Hiya folks.

How do I output the type of a variable?

alpha = {“one”, 1, “two”, 2}
puts alpha.type # this is what I am trying to do

Cheers

By type you want the variable’s class ?

If so use :

puts alpha.class

You might also want to look at this one for more information on how to
use a variable :

puts alpha.methods

Object#class() is the method you are looking for.

On Wed, Oct 5, 2011 at 3:40 PM, Viaduct Productions <
[email protected]> wrote:

Rich in Toronto


Sincerely,

Isaac S.
Section C-4B Vice Chief, Order of the Arrow
Vice Chief of Administration, Tecumseh #65
Eagle Scout

On Wed, Oct 5, 2011 at 12:40 PM, Viaduct Productions
[email protected] wrote:

How do I output the type of a variable?

alpha = {“one”, 1, “two”, 2}
puts alpha.type # this is what I am trying to do

“type”? Maybe you actually mean ‘alpha.class’ ?

Thank you sir.


Rich in Toronto

Yes. I come from the world of Lasso. We do the same this way:

var(‘alpha’) = array(1, 2, 3);
$alpha->type; // this outputs the class


Rich in Toronto

Viaduct Productions wrote in post #1025201:

Hi Kassym. Thanks for the post.

puts alpha.inspect I have found doesn’t do anything more than this:

puts alpha

Would that change with more complex compound data types?

That is correct. Inspect returns a string description/representation of
the object.

Hi Kassym. Thanks for the post.

puts alpha.inspect I have found doesn’t do anything more than this:

puts alpha

Would that change with more complex compound data types?

Cheers


Rich in Toronto

Hi Luke. Thanks for the reply.

BTW is there any beef about top-posting in here? I know some people get
all huffy with that. Having to scroll past a quotation is my beef with
bottom posting.

Yeah the syntax and nomenclature is what’s going to bog me down. Once
it’s flushed out (as was with my first little project), it’s all good.
Type vs. Class, Map vs. Hash, those are the things I have to get
syntactic examples down in BBEdit for me to really move forward. I have
to get used to the Core Reference as well.

Some of those methods are quite useful. I was writing some kind of
Rails for the previous language I was using, and inheritance was a bit
of a PITA because abstraction was quite difficult due to the reference
of inherited attributes of an object. So here I am in Rubyland.

Cheers


Rich in Toronto

On Thu, Oct 6, 2011 at 9:56 AM, Viaduct Productions
[email protected] wrote:

BTW is there any beef about top-posting in here? I know some people get all
huffy with that. Having to scroll past a quotation is my beef with bottom posting.

Well, bottom posting does not mean that one has to include the full
message that is replied to. Usually the recommendation is to remove
the stuff which is irrelevant or which is not replied to.

But if you top post then basically you can omit the whole original
message since the reader has to figure anyway what the reply referred
to. Unfortunately this is not what typically happens: people will
just top post a few sentences and leave the rest untouched. Then it’s
better to completely rid of the original message because it’s only
marginally more effort for the reader to go back to a previous
posting. But with proper comments in line and the message pruned
reading is so much simpler. It also shows that the replier bothers to
make an effort to make it easier for his readers. This is a message
of kindness.

Kind regards

robert


Rich in Toronto

On 2011-10-06, at 9:15 AM, Robert K. wrote:

Well, bottom posting does not mean that one has to include the full
message that is replied to. Usually the recommendation is to remove
the stuff which is irrelevant or which is not replied to.

Yes, many ways of doing it. I’ve just been on lists where every third
comment is “do not top post”. Interspersed responses, like this, I’ve
used in the past as well. Even edited posts can get lengthy, given that
people post code snippets, etc. I was just seeing how this list
generally behaves.

But if you top post then basically you can omit the whole original
message since the reader has to figure anyway what the reply referred
to. Unfortunately this is not what typically happens: people will
just top post a few sentences and leave the rest untouched. Then it’s
better to completely rid of the original message because it’s only
marginally more effort for the reader to go back to a previous
posting. But with proper comments in line and the message pruned
reading is so much simpler. It also shows that the replier bothers to
make an effort to make it easier for his readers. This is a message
of kindness.

Yes, there is logic behind it, it’s just that other communities hugely
frown upon such logic, and continue to be that way. Just asking the
questions before the flames come out.

Thanks for the response. It seems this list is reasonably flexible.

Cheers

How do I output the type of a variable?

In Ruby, the type is not really synonymous with the
object’s class, but has more to do with the methods the object
responds to.

So normally Rubyists use class to mean the Class, and ‘type’
is sort of a grey-area word.

But, more practically, object.methods will return all the methods that
object responds to.

object.class.instance_methods(false) will return all methods the object
responds to that were defined in its class but not its class’s
superclass(es).

object.respond_to? :methodname will return true/false depending whether
or
not the object responds to that method name.

object.class.ancestors will return an array of the inheritance chain of
the object’s class (superclasses and mixed in modules).

There’s also object.is_a? ClassName, which is kind of like the ‘type’ of
an object, in that it will respond true if ClassName is a superclass of
object’s class (or mixed in module).

And finally, object.instance_of? ClassName checks if ClassName is the
direct class, but not superclass, of object.

Hope that helps!
(and sorry for the length…)

-Luke

Some devs from a very long time ago (not a Star Wars reference), think
that new submissions to a thread go down below. That, can mean
scrolling through any new posts, which I find something conveniently
left out of the plan. I prefer top posting. As much as quotations are
part of the thread, they are secondary to the current response. Also,
software is designed to do just that, as is this message.

So, not so much a logistical issue, but more of a cultural one with weak
reasonability. Depends who you ask.

Cheers


Rich in Toronto

A: Because it messes with the readability of the reply, since the
reader has to figure out what points you are addressing.
Q: Why is top posting bad?

On Thu, Oct 6, 2011 at 9:56 AM, Viaduct Productions
[email protected] wrote:

BTW is there any beef about top-posting in here? I know some people get all
huffy with that. Having
to scroll past a quotation is my beef with bottom posting.

“The needs of the many outweigh the needs of the few.” - Mr. Spock and
countless Spock wannabes in Trek canon.


Phillip G.

gplus.to/phgaw | twitter.com/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz

On Thu, Oct 6, 2011 at 3:42 PM, Viaduct Productions
[email protected] wrote:

Some devs from a very long time ago (not a Star Wars reference), think that new
submissions to a thread go down
below.

s/Some devs/Most people/ && s/from a very long time ago (not a Star
Wars reference),//;

That, can mean scrolling through any new posts, which I find something
conveniently left out of the plan.

You have to do that anyway, by definition. That’s why it is called
“reading”.

I prefer top posting. As much as quotations are part of the thread, they are
secondary to the current response.

I prefer to be polite, and not waste my reader’s time with creating
unnecessary overhead for them.

Also, software is designed to do just that, as is this message.

It’s much, much easier for me (and, most of all, it’s a one time
cost!), to add someone to a killfile, than to puzzle out what they are
on about.


Phillip G.

gplus.to/phgaw | twitter.com/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
– Leibniz


Rich in Toronto

On 2011-10-06, at 9:55 AM, Phillip G. wrote:

s/Some devs/Most people/ && s/from a very long time ago (not a Star
Wars reference),//;

I don’t understand that.

That, can mean scrolling through any new posts, which I find something
conveniently left out of the plan.

You have to do that anyway, by definition. That’s why it is called “reading”.

No, if it was in a previous post, then there’s no reason to re-read it,
especially if the original author wrote it, or is keeping up on the
thread. It’s called “memory”.

I prefer top posting. As much as quotations are part of the thread, they are
secondary to the current response.

I prefer to be polite, and not waste my reader’s time with creating
unnecessary overhead for them.

But you just said you want people to read. And that you haven’t done to
my last post. I also prefer top-posting and minimizing scrolling.

Also, software is designed to do just that, as is this message.

It’s much, much easier for me (and, most of all, it’s a one time
cost!), to add someone to a killfile, than to puzzle out what they are
on about.

Not interested what’s easier for you. If you are unclear, go back and
read the thread.

Am I right in thinking that you are using trimmed quotes with inline
responses, while saying that including quotes is secondary to the
response?
Come on.

It’s clear that having a quote when you’re responding to something in
particular eases reading comprehension, for both those following the
discussion intently and those less familiar with what is happening.

But “not interested in what’s easier for you” says a lot, anyway.
On Oct 6, 2011 3:04 PM, “Viaduct Productions”
[email protected]
wrote:

Wars reference),//;

I don’t understand that.

That, can mean scrolling through any new posts, which I find something
conveniently left out of the plan.

You have to do that anyway, by definition. That’s why it is called
“reading”.

No, if it was in a previous post, then there’s no reason to re-read it,
especially if the original author wrote it, or is keeping up on the
thread.
It’s called “memory”.

I prefer top posting. As much as quotations are part of the thread, they
are secondary to the current response.

I prefer to be polite, and not waste my reader’s time with creating
unnecessary overhead for them.

But you just said you want people to read. And that you haven’t done to my
last post. I also prefer top-posting and minimizing scrolling.

Also, software is designed to do just that, as is this message.

It’s much, much easier for me (and, most of all, it’s a one time
cost!), to add someone to a killfile, than to puzzle out what they are
on about.

Not interested what’s easier for you. If you are unclear, go back and read
the thread.

And of course I forget to untick “include text”, causing a top-post
reply.
Damn you, Muphry’s Law.

On Thu, Oct 6, 2011 at 3:17 PM, Viaduct Productions <
[email protected]> wrote:

So this is how you think responses should be? Because you want to re-read
what you just submitted to the list? I’m using whatever the list wants.

The simple answer to finding out what the list wants: lurk.

If you want my preference: trim quotes and bottom-/inline-post. Consider
the
situation where someone makes 3 complex points. Compare the extra effort
to
all future readers to piece together what’s being responded to, compared
to
a one-off cost of making it clear what is being responded to.

It’s not about making people re-read quoted text. The quoted text is a
marker. Anyone who already read it gets their memory jogged and can
refer
back to it easily (if necessary, compared to navigating back through N
previous replies). Anyone who hasn’t read it gets to see what’s being
discussed.


Rich in Toronto

On 2011-10-06, at 10:11 AM, Adam P. wrote:

Am I right in thinking that you are using trimmed quotes with inline
responses, while saying that including quotes is secondary to the response?
Come on.

It’s clear that having a quote when you’re responding to something in
particular eases reading comprehension, for both those following the
discussion intently and those less familiar with what is happening.

But “not interested in what’s easier for you” says a lot, anyway.

So this is how you think responses should be? Because you want to
re-read what you just submitted to the list? I’m using whatever the
list wants. I don’t really care, to be honest. Some lists won’t even
reply if you don’t choose their method.

I don’t need reading comprehension for a post I submitted myself.
Second, I don’t need it above the text of someone that replies to me.
Because if that is the case, then the thread will get longer and longer,
because people need a full reference of the thread in the past, above
any new material. That, is defeating the purpose of a thread.

Anybody posting in a list should follow the culture, not individuals
that haven’t seen this before. The point of the question, regardless of
how it’s been quoted, has evaded both of you. So no method of quoting
has clarified anything.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs