Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Exclude Proc#source Enclosure #26

Open

Description

@bryanp

Hit a case where I need the Proc source without the enclosure. For example:

p = Proc.new do
  puts 'proc'
end

p.source currently gives me all three lines, but I really want:

puts 'proc'

There doesn't appear to be a way to do this. Possible to add?

Activity

  1. ConradIrwin commented on Oct 21, 2013

    @ConradIrwin
    Collaborator

    It'll be quite hard. The way we currently do this is find the first line of the method, and then extract one complete expression from ruby. To get it without the begin end, it might be sufficient to drop the last end, and delete everything up to the first do or {; but there'll obviously be edge-cases.

  2. bryanp commented on Oct 21, 2013

    @bryanp
    Author

    Eh, that's what I was afraid of. I may work on a solution for easy cases. If so I'll be sure to send a PR along.

  3. banister commented on Oct 21, 2013

    @banister
    Owner

    There's a crazy gem called sourcify which uses its own ragel scanner to do this accurately, you might get some mileague with that ;)

  4. bryanp commented on Oct 22, 2013

    @bryanp
    Author

    Tried sourcify before method_source. It failed anytime a string was defined with double quotes.

    Simple is better, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions