alternatively you could make the two inputs variables and the output you are
trying to recieve a variable as well that way you would never have to worry
about regexing anything. You would simply reference the variable . . . You
can of course create a class as well . . .
puts "First Number"
r = gets.chomp
puts "Second Number"
p = gets.chomp
x = p * r
puts x
class Someclass
def initialize (First Number, Second Number) @First Number = First Number @Second Number = Second Number
end
end
Calc = Someclass.new(0,1)
x = calc.first * calc.last
Of course if you're working with floats or strings back and forth just add
the to_f or to_s. I haven't worked on ruby in a while but maybe this will
help you spawn some ideas.
···
On Wed, Aug 10, 2011 at 1:30 PM, Chris White <cwprogram@live.com> wrote:
On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> Hi,
>
> Please help me with regular expression.
>
> str = "abc = 100.300"
>
> i want to write a regular xpression for getting the value after =, i.e.
> 100.300, i mean reg ex should return value after =
I'd use split as well, but I'd probably do it like this:
str = "abc = 100.300"
variable, value = str.split('=', 2).map(&:strip)
variable # => "abc"
value # => "100.300"
···
On Wed, Aug 10, 2011 at 1:30 PM, Chris White <cwprogram@live.com> wrote:
On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> Hi,
>
> Please help me with regular expression.
>
> str = "abc = 100.300"
>
> i want to write a regular xpression for getting the value after =, i.e.
> 100.300, i mean reg ex should return value after =
Um, no, why would you need a class to store two numbers? I'd go with
Chris's split approach. Unless what you really want is to match the
number, then you do need regex and an example one would be /\d+\.\d+/
.
···
2011/8/10, Daniel Navarro <djonavarro@gmail.com>:
alternatively you could make the two inputs variables and the output you are
trying to recieve a variable as well that way you would never have to worry
about regexing anything. You would simply reference the variable . . . You
can of course create a class as well . . .
puts "First Number"
r = gets.chomp
puts "Second Number"
p = gets.chomp
x = p * r
puts x
class Someclass
def initialize (First Number, Second Number) @First Number = First Number @Second Number = Second Number
end
end
Calc = Someclass.new(0,1)
x = calc.first * calc.last
Of course if you're working with floats or strings back and forth just add
the to_f or to_s. I haven't worked on ruby in a while but maybe this will
help you spawn some ideas.
On Wed, Aug 10, 2011 at 1:30 PM, Chris White <cwprogram@live.com> wrote:
On Aug 10, 2011, at 11:20 AM, saurabh a. wrote:
> Hi,
>
> Please help me with regular expression.
>
> str = "abc = 100.300"
>
> i want to write a regular xpression for getting the value after =, i.e.
> 100.300, i mean reg ex should return value after =
It seems to me that there could be whitespace on either end, ie
"
def whatever
var = 123
end
"
This shows a very common example of leading whitespace, so I thought using
strip would be better.
···
On Wed, Aug 10, 2011 at 7:44 PM, Chad Perrin <code@apotheon.net> wrote:
On Thu, Aug 11, 2011 at 07:34:54AM +0900, Josh Cheek wrote:
>
> I'd use split as well, but I'd probably do it like this:
>
> str = "abc = 100.300"
> variable, value = str.split('=', 2).map(&:strip)
> variable # => "abc"
> value # => "100.300"
I'd probably use a regex for the split sequence rather than a literal to
account for variations in whitespace, rather than a map:
Although, I suppose you could make the argument that an actual parser would
be best.
···
On Wed, Aug 10, 2011 at 8:15 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Wed, Aug 10, 2011 at 7:44 PM, Chad Perrin <code@apotheon.net> wrote:
On Thu, Aug 11, 2011 at 07:34:54AM +0900, Josh Cheek wrote:
>
> I'd use split as well, but I'd probably do it like this:
>
> str = "abc = 100.300"
> variable, value = str.split('=', 2).map(&:strip)
> variable # => "abc"
> value # => "100.300"
I'd probably use a regex for the split sequence rather than a literal to
account for variations in whitespace, rather than a map: