Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 902091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:39:40+00:00 2026-05-15T15:39:40+00:00

I’m trying to do some scripted GUI testing in Windows using Ruby. I’m leveraging

  • 0

I’m trying to do some scripted GUI testing in Windows using Ruby. I’m leveraging the guidance of a pragprog book and I’m hitting a bit of a snag. I’ve factored out some code to help with the repeatitive mapping of win32api functions. The code is as follows:

module WindowsGui

  def self.def_api(function, parameters, return_value)              
    api = Win32API.new 'user32', function, parameters, return_value

    define_method(function.snake_case) do |*args|                  
      api.call *args                                                
    end

  end
end

…So I can use that block to define several win32APIs in the module in the following fashion:

def_api 'FindWindow', ['P', 'P'], 'L' 

I’ve got some RSpec tests to ensure this behaves as it should.

  it 'wraps a Windows call with a method' do
    find_window(nil, nil).should_not == 0
  end
  
  it 'enforces the argument count' do
    lambda {find_window}.should raise_error
  end

Everything works fine as for the first test, the second results in a seg fault. Seems that if I call this with no args, I can’t touch *args – but I can do a args.length to see that it’s empty.

Why would this result in a seg fault and not an exception? Would it be acceptable to resolve this through something like..

  raise BadArgs if args.length == 0

Yet another question that is way too long – sorry for that.

TIA!
Bob

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:39:41+00:00Added an answer on May 15, 2026 at 3:39 pm

    The likely reason it’s seg faulting is because somewhere underneath Win32API the lack of arguments is translating into one or more NULL pointer dereferences.

    If you are going to create functions like this then you should consider adding a per-api-call, boolean-returning argument validation block (or lambda) as an argument to def_api which you would then invoke as the first part of your derived function. Something like this:

    def self.def_api(function, parameters, return_value, &valid_args?)              
      api = Win32API.new 'user32', function, parameters, return_value
    
      define_method(function.snake_case) do |*args|
        if valid_args? and valid_args?.call(*args)               
          api.call *args
        else
          // raise argument error, etc
        end                                         
      end
    
    end
    

    Then

    def_api 'FindWindow', ['P', 'P'], 'L', { |args| return true if // API specific checks succeed }
    

    UPDATE: Adding more color at questioner’s request

    The &valid_args? is the name of a block parameter. The ampersand prefix (&) is how you tell ruby that you passing a block. You can only pass one block to a methodand it must be the last parameter in the argument list. The question mark suffix (?) is just a convention in Ruby programming for naming functions which return boolean values.

    If a block is passed you invoke it with &block.call(args)

    To call a method with a block argument you do something like this:

    method { |args| // block implementation }
    

    or

    method do |args|
      // block implementation
    end
    

    The args are passed to the block through the call method. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If your end users are all using Windows and are… May 16, 2026 at 8:25 am
  • Editorial Team
    Editorial Team added an answer I think you want to use the DataLoadOptions.LoadWith method, described… May 16, 2026 at 8:25 am
  • Editorial Team
    Editorial Team added an answer You can work remote, but you cannot do Version Control… May 16, 2026 at 8:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.