DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Introduction to Domain-Driven Design
  • 10 Traits That Separate the Best Devs From the Crowd
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Manifold vs. Lombok: Enhancing Java With Property Support

Trending

  • Introduction to Domain-Driven Design
  • 10 Traits That Separate the Best Devs From the Crowd
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Manifold vs. Lombok: Enhancing Java With Property Support

Ruby Ranges As Conditional Expressions

Snippets Manager user avatar by
Snippets Manager
·
May. 03, 10 · Code Snippet
Like (0)
Save
Tweet
Share
723 Views

Join the DZone community and get the full member experience.

Join For Free
Source: Ruby Ranges - Techotopia [techotopia.com]

Ruby Ranges can also be used as conditional expressions in looping conditions. The range start value represents the start of the loop, which runs until the range end marker is detected.

while input = gets
  puts input + " triggered" if input =~ /start/ .. input =~ /end/
end


output (test performed within an IRB session):
test
ready
go
do it
start
start
 triggered
fun fun fun
fun fun fun
 triggered
rest
rest
 triggered
that's enough
that's enough
 triggered
stop
stop
 triggered
end
end
 triggered
sigh
hello?
^CIRB::Abort: abort then interrupt!!
Note: This syntax also works: while input = gets puts input + " triggered" if input[/start/] .. input[/end/] end

Opinions expressed by DZone contributors are their own.

Trending

  • Introduction to Domain-Driven Design
  • 10 Traits That Separate the Best Devs From the Crowd
  • AI and Cybersecurity Protecting Against Emerging Threats
  • Manifold vs. Lombok: Enhancing Java With Property Support

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: