问题列表 - 第39172页

Ruby的"Open Classes"打破了封装吗?

在Ruby中,程序员可以更改预定义的类.所以一个非常糟糕的程序员可以做类似的事情:

class String
  def ==(other)
    return true
  end
end
Run Code Online (Sandbox Code Playgroud)

显然,几乎没有人会这么愚蠢,但是对于预定义类的更微妙的改变可能会导致已经工作的代码出现问题的想法在我看来违反了封装原则.

四个问题:

  1. 首先,这实际上是否违反了OO封装原则?
  2. 第二,作为一名程序员,我是否有办法在我的代码中保证我正在使用未修改版本的类?
  3. 第三,我是否应该出于任何原因在我的代码中"打开"类?
  4. 最后,在大规模的生产编码环境中如何处理这类事情?换句话说,编程行业的人们是否真的在其他人会使用的代码中执行此操作?或者即使他们不这样做,你如何确保一些插件作者在某个地方没有做到这样会破坏你程序的重要部分呢?

我知道这是一个有点主观的问题,但我真的想知道更广泛的编程社区对这个所谓的"猴子修补"的看法.

ruby encapsulation monkeypatching

9
推荐指数
1
解决办法
734
查看次数

如何使用正则表达式在iOS中获取匹配?

我有一个像'stackoverflow.html'这样的字符串,在正则表达式'stack(.).html'中我希望得到(.)中的值.

我只能找到NSPredicate:

NSString    *string     = @"stackoverflow.html";
NSString    *expression = @"stack(.*).html";
NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", expression];
BOOL match = [predicate evaluateWithObject:string]
Run Code Online (Sandbox Code Playgroud)

但是当我使用NSRegularExpression时,这只会告诉我有一个匹配并且不返回字符串:

NSRange range = [string rangeOfString:expression options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
if (range.location == NSNotFound) return nil;

NSLog (@"%@", [string substringWithRange:(NSRange){range.location, range.length}]);
Run Code Online (Sandbox Code Playgroud)

它会给我一个完整的字符串,stackoverflow.html,但我只对(.*)中的那些感兴趣.我想要'溢出'回来.在PHP中这很容易做到,但是如何在xCode for iOS中实现这一点?

从逻辑上讲,如果我这样做:

NSInteger firstPartLength  = 5;
NSInteger secondPartLength = 5;
NSLog (@"%@", [string substringWithRange:(NSRange){range.location + firstPartLength, range.length - (firstPartLength + secondPartLength)}]
Run Code Online (Sandbox Code Playgroud)

它给了我属性结果'溢出'.但问题是在很多情况下我不知道第一部分或第二部分的长度.那么有没有办法让我得到应该在(.*)的值?

或者我必须通过找到(.)的位置并从那里计算第一和第二部分来决定选择最丑的方法吗?但是在正则表达式中你可能也有([az])但是然后用丑陋的方式使用另一个正则表达式获取()之间的值的位置然后用它来计算左右部分?如果我有更多,会发生什么?比如'A(. …

regex iphone xcode

25
推荐指数
2
解决办法
4万
查看次数

是否有一个XPath表达式可用于在CDATA部分中导航XML?

我正在试图弄清楚如何使用XPath从以下XML文档中的XML片段中获取exceptionID和instrumentID值(是的,CDATA中的XML有点奇怪,但这是我从第三方服务获得的)

<?xml version="1.0"?>
  <exception>
    <info>
      <![CDATA[
        <info>
          <exceptionID>1</exceptionID>
          <instrumentID>1</instrumentID>
        </info>
      ]]>
    </info>
</exception>
Run Code Online (Sandbox Code Playgroud)

是否可以在一个XPath语句中获取值?

我在Java中使用javax.xml.xpath.XPath(JDK 1.5 with Xalan 2.7.1和Xerces 2.9.1),例如

XPath xpath = XPathFactory.newInstance().newXPath();

Long exceptionId  = new Long(((Double)xpath.evaluate(this.exceptionIdXPath, 
                               document, XPathConstants.NUMBER)).longValue());
Run Code Online (Sandbox Code Playgroud)

这是this.exceptionIdXPath变量,我不知道如何设置,我知道例如:

/exception/info/text()/info/exceptionID 将无法正常工作(text()返回CDATA内的数据,但没有"知识"它是XML)

java xml xpath

1
推荐指数
1
解决办法
1068
查看次数

如何将PHP脚本伪装成浏览器?

我们一直在使用来自网站的信息一段时间(如果您提及源代码我们这样做,该网站允许的内容)我们一直在手动复制信息.你可以想象这可能会很快变得乏味,所以我一直试图通过PHP脚本获取信息来自动化这个过程.

我正在尝试获取的URL是:

http://mediaforest.ro/weeklycharts/viewchart.aspx?r=WeeklyChartRadioLocal&y=2010&w=46 08-11-10 14-11-10
Run Code Online (Sandbox Code Playgroud)

如果我在浏览器中输入它,它会起作用,如果我尝试使用file_get_contents(),我会收到错误的请求

我想他们检查了客户端是否是浏览器,所以我推出了基于CURL的解决方案:

$ch = curl_init();

$header=array(
  'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12',
  'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language: en-us,en;q=0.5',
  'Accept-Encoding: gzip,deflate',
  'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  'Keep-Alive: 115',
  'Connection: keep-alive',
);

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
$result=curl_exec($ch);

curl_close($ch);
Run Code Online (Sandbox Code Playgroud)

我已经检查过,标题与我的浏览器标题完全相同,我仍然收到错误请求

所以我试了另一个解决方案

http://www.php.net/manual/en/function.curl-setopt.php#78046
Run Code Online (Sandbox Code Playgroud)

不幸的是,这也不起作用,我没有想法.我错过了什么?

php user-agent curl

14
推荐指数
2
解决办法
2万
查看次数

ADODB RecordSet作为Access Report RecordSource

我在Access 2003中有一个简单的表单,一个查询和一个报表.我必须使用VBA操作记录集中的查询结果,然后将其作为RecordSource传递给报表.

如果我将记录集声明为RecordSet并使用其Name属性作为报告的RecordSource,那么它正在工作.但是,因为我需要编辑记录集,所以尽管如下使用ADODB RecordSet会更容易.

记录集Dim rs As ADODB.RecordSet在全局模块中声明.其余的代码是;

Dim db As Database
Set db = CurrentDb
Dim con As ADODB.Connection
Set con = CurrentProject.Connection
Set rs = New ADODB.Recordset
Set rs.ActiveConnection = con
rs.Source = "Select * from XXX"
rs.LockType = adLockOptimistic
rs.CursorType = adOpenKeyset
rs.Open

'manipulate rs here....'
Run Code Online (Sandbox Code Playgroud)

我曾经将报告的RecordSource作为myReport.RecordSource = rs.Name传递.但是ADODB没有Name属性.

如何将此记录集作为其RecordSource传递给报表?

谢谢

ms-access vba adodb recordset access-vba

7
推荐指数
1
解决办法
1万
查看次数

jQuery选择了名字

如果从下拉框中选择Tom,我如何使用jQuery获取名称而不是值,以便得到的输出是Tom?

<select id="emp" >
  <option value=1>Tom</option>
  <option value=12>Harold</option>e 
  <option value=32>Jenny</option>
</select>
Run Code Online (Sandbox Code Playgroud)

jquery jquery-selectors

6
推荐指数
1
解决办法
4267
查看次数

帮助我理解boost :: bind的这种用法

请看一下Johannes Schaub发布的这个例子来排序对的向量:

如何根据对的第二个元素对对向量进行排序?

std::sort(a.begin(), a.end(), 
          boost::bind(&std::pair<int, int>::second, _1) <
          boost::bind(&std::pair<int, int>::second, _2));
Run Code Online (Sandbox Code Playgroud)

我以为我确实理解了boost :: bind,但我遇到了这个问题.

问题1:

排序算法期望谓词函数作为第三个参数.我在这里看到的是一个布尔表达式.我错过了什么?:

boost::bind(&std::pair<int, int>::second, _1) < boost::bind(&std::pair<int, int>::second, _2)
Run Code Online (Sandbox Code Playgroud)

对于那两个绑定,boost :: bind库是否重载operator <并且正在返回某种函数指针(如lambda)?

问题2:
这让我感到困惑:

boost::bind(&std::pair<int, int>::second, _1)
Run Code Online (Sandbox Code Playgroud)

通常有一些函数指针作为绑定调用的第一个参数,但是这里是一个类成员的地址?特定绑定的结果是什么?

感谢您的时间和帮助

c++ boost-bind

15
推荐指数
1
解决办法
2112
查看次数

git cherry-pick -x:详细链接而不是摘要

给定提交消息"foo",即只有摘要部分,我这样做git cherry-pick -x the_commit.结果是带有消息的新提交

foo
(cherry picked from commit eb42a6475d2c2e4fff7a1b626ce6e27eec21e886)
现在这不好,因为它是一个两行摘要,这似乎是git中的一个错误.

但是,如果不手动编辑注释,我如何让git使评论看起来如下?

foo

(cherry picked from commit eb42a6475d2c2e4fff7a1b626ce6e27eec21e886)

git cherry-pick

6
推荐指数
1
解决办法
1029
查看次数

将C Enum转换为delphi?

我为C Enum写了一个delphi数字你能回答我,我在哪里可能有错?有什么不对的吗?

C:

 typedef  enum {
   AttributeStandardInformation = 0x10,
   AttributeAttributeList = 0x20,
   AttributeFileName = 0x30,
   AttributeObjectId = 0x40,
   AttributeSecurityDescriptor = 0x50,
   AttributeVolumeName = 0x60,
   AttributeVolumeInformation = 0x70,
   AttributeData = 0x80,
   AttributeIndexRoot = 0x90,
   AttributeIndexAllocation = 0xA0,
   AttributeBitmap = 0xB0,
   AttributeReparsePoint = 0xC0,
   AttributeEAInformation = 0xD0,
   AttributeEA = 0xE0,
   AttributePropertySet = 0xF0,
   AttributeLoggedUtilityStream = 0x100

} ATTRIBUTE_TYPE
Run Code Online (Sandbox Code Playgroud)

并转换为delphi枚举:

ATTRIBUTE_TYPE=( AttributeStandardInformation = $10,
   AttributeAttributeList = $20,
   AttributeFileName = $30,
   AttributeObjectId = $40,
   AttributeSecurityDescriptor = $50,
   AttributeVolumeName = $60,
   AttributeVolumeInformation = $70,
   AttributeData …
Run Code Online (Sandbox Code Playgroud)

delphi

3
推荐指数
1
解决办法
2116
查看次数

Javascript:从页面内容中将数据下载到文件

设置如下:我有一个主页,我在其中显示使用页面内的逗号分隔值构建的图表.我想让用户可以将数据作为cvs文件下载,而无需再次联系服务器.(因为数据已经存在)这有可能吗?我更喜欢纯JavaScript解决方案.

到目前为止,我已经发现:http://pixelgraphics.us/downloadify/test.html但它涉及闪存,我想避免.

我无法想象以前没有问过这个问题.我很抱歉双重发帖,但似乎我使用了错误的关键字或其他东西 - 我在这些论坛中找不到解决方案.

javascript file local download

22
推荐指数
3
解决办法
4万
查看次数