question on blocks

J

Jeff Thorne

------=_NextPart_000_000F_01C654E5.5149E8C0
Content-Type: text/plain;
charset="us-ascii"
Content-Transfer-Encoding: 7bit

I am new to ruby from java and had a question on blocks.



Can a block be applied to a writeable attribute or setter? I keep getting
errors so I

am assuming no but I wanted to double check in case my syntax is incorrect.



Thanks for the help,

Jeff





class Test



def initialize(one, two)

@one = one

@two = two

end



def one=(one)

@one = one;

Yield;

end



attr_reader :eek:ne, :two

attr_writer :two



end







test = Test.new("Hello", 2);

test.one = 3 { puts "Test Block" }




------=_NextPart_000_000F_01C654E5.5149E8C0--
 
M

Marcin Mielżyński

Can anyone explain this: ?

class A
def x
yield
end

def x=a
yield
end
end

a=A.new
a.x{p "called"}
a.x=1 do
p "called"
end


../test.rb:13: parse error, unexpected kDO, expecting $


lopex
 
P

Patrick Hurley

I am new to ruby from java and had a question on blocks.



Can a block be applied to a writeable attribute or setter? I keep getting
errors so I

am assuming no but I wanted to double check in case my syntax is incorrec= t.



Thanks for the help,

Jeff





class Test



def initialize(one, two)

@one =3D one

@two =3D two

end



def one=3D(one)

@one =3D one;

Yield;

end



attr_reader :eek:ne, :two

attr_writer :two



end







test =3D Test.new("Hello", 2);

test.one =3D 3 { puts "Test Block" }

You are correct it is not allowed, but I am curious why you want to do
this? Also if I were to go and write a setter that took a block,
wouldn't you either pass the new value into the block or use the
result of the block to set the variable?

class Test
def a=3D(a)
@a =3D a
yield @a if block_given?
end

#or
def b=3D(b)
@b =3D (block_given?) ? yield b : b
end
end

pth
 
J

Jeff Thorne

Patrick,

Thanks for the quick response. I agree that it doesn't make sense
unless I was passing the new value back to the block. I am about
15 minutes into the Picaxe and was just trying to get a handle on the
syntax. The language looks great. My brain is just hardwired with java
syntax at this point :)

Cheers,
Jeff


-----Original Message-----
From: Patrick Hurley [mailto:p[email protected]]
Sent: Friday, March 31, 2006 5:49 PM
To: ruby-talk ML
Subject: Re: question on blocks

I am new to ruby from java and had a question on blocks.



Can a block be applied to a writeable attribute or setter? I keep getting
errors so I

am assuming no but I wanted to double check in case my syntax is incorrect.



Thanks for the help,

Jeff





class Test



def initialize(one, two)

@one = one

@two = two

end



def one=(one)

@one = one;

Yield;

end



attr_reader :eek:ne, :two

attr_writer :two



end







test = Test.new("Hello", 2);

test.one = 3 { puts "Test Block" }

You are correct it is not allowed, but I am curious why you want to do
this? Also if I were to go and write a setter that took a block,
wouldn't you either pass the new value into the block or use the
result of the block to set the variable?

class Test
def a=(a)
@a = a
yield @a if block_given?
end

#or
def b=(b)
@b = (block_given?) ? yield b : b
end
end

pth
 
P

Patrick Hurley

You are correct it is not allowed, but I am curious why you want to do
this? Also if I were to go and write a setter that took a block,
wouldn't you either pass the new value into the block or use the
result of the block to set the variable?

class Test
def a=3D(a)
@a =3D a
yield @a if block_given?
end

#or
def b=3D(b)
@b =3D (block_given?) ? yield b : b
end
end

pth

Sorry just to be clear, I do not think that method=3D methods are
allowed to take anything more than a single parameter -- when using
the normal "call" syntax.

Of course you can send a message to it with a block:

class A
attr_reader :a
def a=3D(a,&b)
@a =3D block_given? ? yield(a) : a
end
end

a=3DA.new
a.a =3D 7
p a.a
a.a =3D 1
bl =3D Proc.new { |v| v * 6 }
a.send "a=3D", 7, &bl
p a.a
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top