I've looked around but I couldn't find anything helpful on this,
although I'm sure it's been covered before:
Does Ruby have an equivalent to the "With / End With" syntax helper in
VB? It would be quite helpful when I need to execute a series of methods
on an object, but for various reasons (e.g. testing individual returns
or methods which may return nil) I don't want to chain them together.
To illustrate, here's the VB syntax I'm referring to:
my_object.instance_eval do
# self points to my_object here
do1
do2
do3
end
You can of course use a different method name, i.e. alias #instance_eval to #with.
However, there is one gotcha
foo.bar = 99
cannot be translated to
foo.instance_eval do
bar = 99
end
because bar will be a local variable then. Instead you have to use
foo.instance_eval do
self.bar = 99
end
Kind regards
robert
···
On Sun, Feb 10, 2013 at 9:25 PM, Joel Pearson <lists@ruby-forum.com> wrote:
I've looked around but I couldn't find anything helpful on this,
although I'm sure it's been covered before:
Does Ruby have an equivalent to the "With / End With" syntax helper in
VB? It would be quite helpful when I need to execute a series of methods
on an object, but for various reasons (e.g. testing individual returns
or methods which may return nil) I don't want to chain them together.
To illustrate, here's the VB syntax I'm referring to:
On 11/02/2013, at 9:25 AM, Joel Pearson <lists@ruby-forum.com> wrote:
I've looked around but I couldn't find anything helpful on this,
although I'm sure it's been covered before:
Does Ruby have an equivalent to the "With / End With" syntax helper in
VB? It would be quite helpful when I need to execute a series of methods
on an object, but for various reasons (e.g. testing individual returns
or methods which may return nil) I don't want to chain them together.
To illustrate, here's the VB syntax I'm referring to:
Can this work for constants as well? I have a several constants within a
long class name and I was wondering whether I can use this approach, but
it doesn't seem to be working for me:
irb(main):001:0> class A
irb(main):002:1> B = 1
irb(main):003:1> end
=> 1
irb(main):004:0> A::B
=> 1
irb(main):005:0> A.new.instance_eval do
irb(main):006:1* puts B
irb(main):007:1> end
NameError: uninitialized constant B
from (irb):6:in `block in irb_binding'
from (irb):5:in `instance_eval'
from (irb):5
from C:/Ruby193/bin/irb:12:in `<main>'
"XL" as a class name is my own attempt at simplifying the original
"ExcelConst". I was just wondering whether I could use these constants
without repeating "XL::" all the time. I don't know how to make them
"main" constants, and I assumed that that sort of thing would be bad
practice anyway.
This is more about trying to work with Excel's awkward API, and the way
it insists on specifying every border of every cell as a seperate
constant.
- The value to be tapped is the result of evaluating an expression
(and not already stored in a variable) AND
- It should be used as the result of the larger expression.
Typical example:
module Enumerable
def group_by
{}.tap do |groups|
each do |e|
(groups[yield(e)] ||= ) << e
end
end
end
end
In all other cases doing a simple assignment like
mo = my_object
mo = my.complicated.expression
is probably easier, cleaner and faster.
Kind regards
robert
···
On Mon, Feb 11, 2013 at 10:04 AM, Joel Pearson <lists@ruby-forum.com> wrote:
Nice one Henry. As usual Ruby has more than one way to do everything
How do you want to use that feature (if it existed)? Maybe there's
another elegant solution.
Kind regards
robert
···
On Thu, Mar 7, 2013 at 12:26 PM, Joel Pearson <lists@ruby-forum.com> wrote:
Can this work for constants as well? I have a several constants within a
long class name and I was wondering whether I can use this approach, but
it doesn't seem to be working for me:
irb(main):001:0> class A
irb(main):002:1> B = 1
irb(main):003:1> end
=> 1
irb(main):004:0> A::B
=> 1
irb(main):005:0> A.new.instance_eval do
irb(main):006:1* puts B
irb(main):007:1> end
NameError: uninitialized constant B
from (irb):6:in `block in irb_binding'
from (irb):5:in `instance_eval'
from (irb):5
from C:/Ruby193/bin/irb:12:in `<main>'
On Thu, Mar 7, 2013 at 2:07 PM, Joel Pearson <lists@ruby-forum.com> wrote:
It's part of my continual effort to combine minimialism with readability
in my code.
Here's the snippet of code which is affected:
____________________________
class XL; end
WIN32OLE.const_load( excel, XL )
#Purdy it up
#Align left 2 columns to left
sht.range('A:B').HorizontalAlignment = XL::XlLeft
"XL" as a class name is my own attempt at simplifying the original
"ExcelConst". I was just wondering whether I could use these constants
without repeating "XL::" all the time. I don't know how to make them
"main" constants, and I assumed that that sort of thing would be bad
practice anyway.
This is more about trying to work with Excel's awkward API, and the way
it insists on specifying every border of every cell as a seperate
constant.