[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: object reference handle (like perl's reference to scalar

Peña, Botp

5/6/2005 4:25:00 AM

Eric Mahurin [mailto:eric_mahurin@yahoo.com] wrote:


#Below is what I think is a general purpose solution to making
#references. I provide several ways to do it. Here is an
#example:
#
#a = (0..5).to_a -> [0,1,2,3,4,5]
#w = ref{"a[2..4]"} # uses eval
#x = a.ref[2..4] # uses [] and []= methods
#y = a.ref("[]","[]=",2..4) # specify methods manually
#z = a.ref(["[]",2..4],["[]=",2..4]) # unique args to get/set
#w[] -> [2,3,4]
#x[] -> [2,3,4]
#y[] -> [2,3,4]
#z[] -> [2,3,4]
#w[]=(11..14).to_a
#a -> [0,1,11,12,13,14,5]
#x[]=[4,3,2]
#a -> [0,1,4,3,2,14,5]
#y[]=[0,1]
#a -> [0,1,0,1,14,5]
#z[]=[2,3,4]
#a -> [0,1,2,3,4,5]

cool.

is it possible to remove the [] appendix? :-)

something like,

hacker = ["bot","pen","a",5,10]

name = hacker.ref[0..2]
fname = name.ref[0]
lname = name.ref[1]
mname = name.ref[2]

height = hacker.ref[3..4]
feet = height.ref[1]
inches = height.ref[2]

fname = "Eric"
lname = "Mahurin"
mname = "?"

feet = 6
inches = 5

name #=> ["Eric","Mahurin","?"]
height #=> [6,5]
hacker #=> ["Eric","Mahurin","?",6,5]


Possible?

btw, i cannot download your reference gem...

thanks and kind regards -botp









5 Answers

dave.burt

5/6/2005 6:02:00 AM

0

This is not possible, unfortunately.
In Ruby,
fname = "Eric"
is just an assignment to a local variable. There are no method calls
involved, and nothing to intercept.

You can, of course, go even further and remove the =, leaving:
fname "Eric"
Reminds me of Traits (nee Attributes).
In this case, "fname" would be a method, which means it would not be an
object. You could create a thing like this using code like the
following:

def define_attribute(method_name)
singleton_class = (class << self; self end)
singleton_class.instance_eval do
define_method method_name do |*args|
if args.empty?
instance_variable_get("@#{method_name}")
else
instance_variable_set("@#{method_name}", *args)
end
end
end
singleton_class
end

define_attribute :fname #=> #<Class:#<Object:0x...>>
fname "Eric" #=> "Eric"
fname #=> "Eric"

# or even on an object, OpenStruct-ishly:
foo = "bar"
foo.define_attribute "baz" #=> #<Class:#<String:0x...>>
foo.baz 42 #=> 42
foo.baz #=> 42

But I digress wildly, I fear. Back to references, here's your example
worked with my above ref.rb

# Please add the following line to the top of ref.rb:
require 'delegate'

# on with the example
require 'ref'

hacker = ["bot","pen","a",5,10].map{|x|x.ref}.ref

name = hacker[0..2]
fname = name[0]
lname = name[1]
mname = name[2]

height = hacker[3..4]
feet = height[0] # I corrected an off-by one here
inches = height[1] # and here

fname[] = "Eric" # or like this: fname.ref = "Eric"
lname[] = "Mahurin"
mname[] = "?"

feet[] = 6
inches[] = 5

p name #=> ["Eric","Mahurin","?"]
p height #=> [6,5]
p hacker #=> ["Eric","Mahurin","?",6,5]

plainolamerican

6/12/2012 3:38:00 PM

0

On Jun 11, 10:19 pm, "Mr. Malthus" <malthus...@aol.com> wrote:
> Anybody who only speaks one language is a fucking idiot.
>
> Alguien que solo habla un idioma es idiota.
>
> Der nur eine Sprache kennt ist ein Dummkopf.
>
> Guess I don't se the problem.
>
> I am a lot more worried about animals who have a bunch of children,
> especially if those children are on welfare, foodstamps, Medicaid, or
> what have you.
>
> You know, Catholics, trailer Baptists, meth-tucky residents of various
> cities. Dumbasses who pray to Jeeesus in their one, badly spoken
> language.

Anybody who only speaks one language is a fucking idiot.
---
opinion noted
English is 'the' international language ... none other is needed or
wanted in America.

skm1942

6/12/2012 6:37:00 PM

0

On Jun 12, 8:38 am, plainolamerican <plainolameri...@gmail.com> wrote:
> On Jun 11, 10:19 pm, "Mr. Malthus" <malthus...@aol.com> wrote:
>
> > Anybody who only speaks one language is a fucking idiot.
>
> > Alguien que solo habla un idioma es idiota.
>
> > Der nur eine Sprache kennt ist ein Dummkopf.
>
> > Guess I don't se the problem.
>
> > I am a lot more worried about animals who have a bunch of children,
> > especially if those children are on welfare, foodstamps, Medicaid, or
> > what have you.
>
> > You know, Catholics, trailer Baptists, meth-tucky residents of various
> > cities. Dumbasses who pray to Jeeesus in their one, badly spoken
> > language.
>
> Anybody who only speaks one language is a fucking idiot.
> ---
> opinion noted
> English is 'the' international language ... none other is needed or
> wanted in America.

Yes, and many Mexicans and other Latinos both here and in their own
countries only speak one language.....Spanish. Pot, kettle, black.

Mr. Malthus

6/17/2012 5:05:00 AM

0


> ---
> as opposed to myth believing supremacist jews and stinking muzzies.
>
> choose sides carefully

I don't choose ANY of them. There is no god. Muhammad was a liar, Jesus
is fake, and Moses is a myth.

Humanity needs to outgrow needing imaginary friends.

Ramon F Herrera

6/17/2012 11:50:00 AM

0

On Jun 12, 10:38 am, plainolamerican <plainolameri...@gmail.com>
wrote:
> On Jun 11, 10:19 pm, "Mr. Malthus" <malthus...@aol.com> wrote:
>
> > Anybody who only speaks one language is a fucking idiot.
>
> > Alguien que solo habla un idioma es idiota.
>
> > Der nur eine Sprache kennt ist ein Dummkopf.
>
> > Guess I don't se the problem.
>
> > I am a lot more worried about animals who have a bunch of children,
> > especially if those children are on welfare, foodstamps, Medicaid, or
> > what have you.
>
> > You know, Catholics, trailer Baptists, meth-tucky residents of various
> > cities. Dumbasses who pray to Jeeesus in their one, badly spoken
> > language.
>
> Anybody who only speaks one language is a fucking idiot.
> ---

> opinion noted
> English is 'the' international language ... none other is needed or
> wanted in America.

Ah! Not only uneducated and ignorant, but proud of it AND supported by
the other alike, Sandy Myers.

The increase of immigrants should cure that problem.

-Ramon